/** * Copyright (C) 2004 Orbeon, Inc. * * This program is free software; you can redistribute it and/or modify it under the terms of the * GNU Lesser General Public License as published by the Free Software Foundation; either version * 2.1 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The full text of the license is available at http://www.gnu.org/copyleft/lesser.html */ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.orbeon.oxf.pipeline.api.PipelineContext; import org.xml.sax.ContentHandler; import org.xml.sax.Attributes; import org.xml.sax.helpers.AttributesImpl; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.QName; import org.orbeon.oxf.common.OXFException; import org.orbeon.oxf.processor.*; import org.orbeon.oxf.util.PipelineUtils; import org.orbeon.oxf.xml.TransformerUtils; import org.orbeon.oxf.xml.XMLConstants; import org.xml.sax.SAXException; import org.orbeon.oxf.pipeline.api.XMLReceiver; import javax.xml.validation.Schema; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import com.kpn.fiber.portal.stub.co.FrontDeskStub; import com.kpn.fiber.portal.stub.co.data.Customer; import com.kpn.fiber.portal.stub.co.data.GetCustomerDetails; import com.kpn.fiber.portal.stub.co.data.DocumentList; import com.kpn.fiber.portal.stub.co.data.GetInvoiceDocuments; public class ColdFormValidation extends SimpleProcessor { private static final Attributes NO_ATTRIBUTES = new AttributesImpl(); public ColdFormValidation() { addOutputInfo(new ProcessorInputOutputInfo(OUTPUT_DATA)); } public void generateData(PipelineContext context, XMLReceiver xmlReceiver) throws SAXException { /* contentHandler.startDocument(); contentHandler.startElement("", "form", "form", NO_ATTRIBUTES); */ System.out.println("lingesh"); final Document dataInputDocument = readInputAsDOM4J(context, "data"); String errorText=""; boolean isError = false; try { System.out.println("lingesh"); Element rootElement = dataInputDocument.getRootElement(); Element customerNumberElement = rootElement.element("CustomerNumber"); Element errorElement = rootElement.element("Error"); Element complaintElement = rootElement.element("complaint"); Element complaintTypeElement = complaintElement.element("complainttype"); Element messageElement = complaintElement.element("message"); Element contactTypeElement = complaintElement.element("contacttype"); Element emailElement = complaintElement.element("email"); Element telephoneElement = complaintElement.element("telephone"); Element messageErrorElement = complaintElement.element("messageerror"); Element complaintTypeErrorElement = complaintElement.element("complainttypeerror"); Element emailErrorElement = complaintElement.element("emailerror"); Element telephoneErrorElement = complaintElement.element("telephoneerror"); Pattern messagePattern = Pattern.compile("[A-Za-z0-9\\s]{10,1500}"); Pattern emailPattern = Pattern.compile("[a-zA-Z0-9\\!\\#\\$\\%\\*\\/\\?\\|\\^\\{\\}\\`\\~\\u0026\\’\\+\\-\\=\\_\\.]+\\@([a-zA-Z0-9\\-]+\\.)+[a-zA-Z0-9]{2,10}"); Pattern telephonePattern = Pattern.compile("[0][1-9][0-9]{8}"); Pattern telephonePattern1 = Pattern.compile("[0][8][1-9][0-9]{7}"); Pattern telephonePattern2 = Pattern.compile("[0][1,2,3,4,5,6,7][0-9]{8}"); /*Complaint type validation*/ int complaintTypeLength = complaintTypeElement.getStringValue().length(); String complaintTypeErrorText = ""; if( complaintTypeLength == 0 ) { complaintTypeErrorText = "Please select the complaint type"; isError = true; } else { complaintTypeErrorText = ""; } complaintTypeErrorElement.setText( complaintTypeErrorText ); /*Message validation*/ Matcher messageMatcher = messagePattern.matcher(messageElement.getStringValue()); boolean validMessage = messageMatcher.matches(); int messageLength = messageElement.getStringValue().length(); boolean messageerror = false; String messageErrorText =""; if(!messageerror) { if(messageLength == 0) { messageErrorText = "Message is mandatory"; messageerror = true; isError = true; } else { messageErrorText = ""; } } if(!messageerror) { if(messageLength < 10) { messageErrorText = "Message should be min of 10"; messageerror = true; isError = true; } else { messageErrorText = ""; } } if(!messageerror) { if(messageLength > 1500) { messageErrorText = "Message should be max of 1500 characters"; messageerror = true; isError = true; } else { messageErrorText = ""; } } messageErrorElement.setText( messageErrorText ); /*Email validation*/ Matcher emailMatcher = emailPattern.matcher(emailElement.getStringValue()); boolean validEmail = emailMatcher.matches(); boolean emailerror1 =false; int emaillength = emailElement.getStringValue().length(); String emailErrorText =""; if(!emailerror1) { if(emaillength == 0) { emailErrorText = "Email is mandatory"; isError = true; emailerror1 = true; } else { emailErrorText = ""; } } if(!emailerror1) { if(!validEmail) { emailErrorText = "Please provide a valid Email eg(abc@xyz.com)"; isError = true; emailerror1 = true; } else { emailErrorText = ""; } } if(!emailerror1) { if(emaillength > 50) { emailErrorText = "Email should not be more than 50 characters"; isError = true; emailerror1 = true; } else { emailErrorText = ""; } } emailErrorElement.setText( emailErrorText ); /* Telephone Validation */ Matcher telephoneMatcher = telephonePattern.matcher(telephoneElement.getStringValue()); Matcher telephoneMatcher1 = telephonePattern1.matcher(telephoneElement.getStringValue()); Matcher telephoneMatcher2 = telephonePattern2.matcher(telephoneElement.getStringValue()); boolean validTelephone = telephoneMatcher.matches(); boolean validTelephone1 = telephoneMatcher1.matches(); boolean validTelephone2 = telephoneMatcher2.matches(); boolean teleerror =false; System.out.println(contactTypeElement.getStringValue()); String contactTypeValue = contactTypeElement.getStringValue(); String telephoneErrorText = ""; int telephonelength = telephoneElement.getStringValue().length(); if(!teleerror) { if("Phone".equals(contactTypeValue)) { if(telephonelength == 0) { telephoneErrorText = "Telephone is mandatory"; isError = true; teleerror = true; } else { telephoneErrorText = ""; } } } if(telephonelength != 0) { if(!teleerror) { if(!validTelephone) { telephoneErrorText = "Start with zero and max of 10"; isError = true; teleerror = true; } else { telephoneErrorText = ""; } } if(!teleerror) { if(!validTelephone1 && !validTelephone2) { telephoneErrorText = "Please provide a valid kpn number"; isError = true; teleerror = true; } else { telephoneErrorText = ""; } } } telephoneErrorElement.setText( telephoneErrorText ); if(errorElement != null) { if(isError) { errorText = "true"; errorElement.setText( errorText ); } else { errorText = "false"; errorElement.setText( errorText ); } } // Serialize result to output TransformerUtils.writeDom4j( dataInputDocument, xmlReceiver ); } catch (Exception e) { errorText = "true"; System.out.println(e); /*contentHandler.startElement("", "Error", "Error", NO_ATTRIBUTES); contentHandler.characters(errorText.toCharArray(), 0, errorText.length()); contentHandler.endElement("", "Error", "Error");*/ } /* contentHandler.endElement("", "form", "form"); contentHandler.endDocument();*/ } }