public String AsXml() { XmlOptions opt = (new XmlOptions()).setSavePrettyPrint(); opt.setSaveSuggestedPrefixes(Utilities.SuggestedNamespaces()); opt.setSaveNamespacesFirst(); opt.setSaveAggressiveNamespaces(); opt.setUseDefaultNamespace(); DescribeSRSResponseDocument document = completeResponse(); ArrayList errorList = new ArrayList(); opt.setErrorListener(errorList); boolean isValid = document.validate(opt); // If the XML isn't valid, loop through the listener's contents, // printing contained messages. if (!isValid) { for (int i = 0; i < errorList.size(); i++) { XmlError error = (XmlError) errorList.get(i); System.out.println("\n"); System.out.println("Message: " + error.getMessage() + "\n"); System.out.println( "Location of invalid XML: " + error.getCursorLocation().xmlText() + "\n"); } } return document.xmlText(opt); }
/** * Validates the XML structure. * * @param xml input xml. * @return list of errors. */ public static List<String> validateXml(String xml) { WsdlValidator validator = new WsdlValidator(null); List<XmlError> errorList = new ArrayList<XmlError>(); validator.validateXml(xml, errorList); List<String> errors = new ArrayList<String>(); for (XmlError error : errorList) { errors.add("Line " + error.getLine() + ": " + error.getMessage()); } return errors; }
protected void validateXmlDocument(XmlObject xmlDocument) { ArrayList<XmlError> validationErrors = new ArrayList<XmlError>(); XmlOptions validationOptions = new XmlOptions(); validationOptions.setErrorListener(validationErrors); xmlDocument.validate(validationOptions); List<String> excludeErrors = new ArrayList<String>(); excludeErrors.add("Expected element '_Feature@http://www.opengis.net/gml' instead of"); excludeErrors.add("Expected element 'InsertionMetadata@http://www.opengis.net/swes/2.0'"); excludeErrors.add( "Expected element 'AbstractFeature@http://www.opengis.net/gml/3.2' instead of 'SF_SpatialSamplingFeature@http://www.opengis.net/samplingSpatial/2.0'"); for (XmlError validationError : validationErrors) { boolean skip = false; for (String excludeError : excludeErrors) { if (validationError.getMessage().startsWith(excludeError)) { skip = true; } } if (!skip) { fail(validationError.getMessage()); } } }
/* * TODO Replace this version with a method that uses LaxValidationCases and provides means to access the errors after validating the document */ public static boolean validateDocument(final XmlObject doc) throws OwsExceptionReport { // Create an XmlOptions instance and set the error listener. final LinkedList<XmlError> validationErrors = new LinkedList<XmlError>(); final XmlOptions validationOptions = new XmlOptions() .setErrorListener(validationErrors) .setLoadLineNumbers(XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT); // Validate the GetCapabilitiesRequest XML document final boolean isValid = doc.validate(validationOptions); // Create Exception with error message if the xml document is invalid if (!isValid) { String message = null; // getValidation error and throw service exception for the first // error final Iterator<XmlError> iter = validationErrors.iterator(); final List<XmlError> shouldPassErrors = new LinkedList<XmlError>(); final List<XmlError> errors = new LinkedList<XmlError>(); while (iter.hasNext()) { final XmlError error = iter.next(); boolean shouldPass = false; if (error instanceof XmlValidationError) { for (final LaxValidationCase lvc : LaxValidationCase.values()) { if (lvc.shouldPass((XmlValidationError) error)) { shouldPass = true; LOGGER.debug("Lax validation case found for XML validation error: {}", error); break; } } } if (shouldPass) { shouldPassErrors.add(error); } else { errors.add(error); } } final CompositeOwsException exceptions = new CompositeOwsException(); for (final XmlError error : errors) { // get name of the missing or invalid parameter message = error.getMessage(); if (message != null) { exceptions.add( new InvalidRequestException() .at(message) .withMessage("[XmlBeans validation error:] %s", message)); /* * TODO check if code can be used for validation of SOS * 1.0.0 requests // check, if parameter is missing or value * of parameter // is // invalid to ensure, that correct // * exceptioncode in exception response is used * * // invalid parameter value if * (message.startsWith("The value")) { exCode = * OwsExceptionCode.InvalidParameterValue; * * // split message string to get attribute name String[] * messAndAttribute = message.split("attribute '"); if * (messAndAttribute.length == 2) { parameterName = * messAndAttribute[1].replace("'", ""); } } * * // invalid enumeration value --> InvalidParameterValue * else if * (message.contains("not a valid enumeration value")) { * exCode = OwsExceptionCode.InvalidParameterValue; * * // get attribute name String[] messAndAttribute = * message.split(" "); parameterName = messAndAttribute[10]; * } * * // mandatory attribute is missing --> // * missingParameterValue else if * (message.startsWith("Expected attribute")) { exCode = * OwsExceptionCode.MissingParameterValue; * * // get attribute name String[] messAndAttribute = * message.split("attribute: "); if (messAndAttribute.length * == 2) { String[] attrAndRest = * messAndAttribute[1].split(" in"); if (attrAndRest.length * == 2) { parameterName = attrAndRest[0]; } } } * * // mandatory element is missing --> // * missingParameterValue else if * (message.startsWith("Expected element")) { exCode = * SwesExceptionCode.InvalidRequest; * * // get element name String[] messAndElements = * message.split(" '"); if (messAndElements.length >= 2) { * String elements = messAndElements[1]; if * (elements.contains("offering")) { parameterName = * "offering"; } else if * (elements.contains("observedProperty")) { parameterName = * "observedProperty"; } else if * (elements.contains("responseFormat")) { parameterName = * "responseFormat"; } else if * (elements.contains("procedure")) { parameterName = * "procedure"; } else if * (elements.contains("featureOfInterest")) { parameterName * = "featureOfInterest"; } else { // TODO check if other * elements are invalid } } } // invalidParameterValue else * if (message.startsWith("Element")) { exCode = * OwsExceptionCode.InvalidParameterValue; * * // get element name String[] messAndElements = * message.split(" '"); if (messAndElements.length >= 2) { * String elements = messAndElements[1]; if * (elements.contains("offering")) { parameterName = * "offering"; } else if * (elements.contains("observedProperty")) { parameterName = * "observedProperty"; } else if * (elements.contains("responseFormat")) { parameterName = * "responseFormat"; } else if * (elements.contains("procedure")) { parameterName = * "procedure"; } else if * (elements.contains("featureOfInterest")) { parameterName * = "featureOfInterest"; } else { // TODO check if other * elements are invalid } } } else { // create service * exception OwsExceptionReport se = new * OwsExceptionReport(); * se.addCodedException(SwesExceptionCode.InvalidRequest, * message, "[XmlBeans validation error:] " + message); * LOGGER.error("The request is invalid!", se); throw se; } * * // create service exception OwsExceptionReport se = new * OwsExceptionReport(); se.addCodedException(exCode, * message, "[XmlBeans validation error:] " + message); * LOGGER.error("The request is invalid!", se); throw se; */ } } exceptions.throwIfNotEmpty(); } return isValid; }