Java Code Examples for org.apache.xmlbeans.XmlCursor#toParent()
The following examples show how to use
org.apache.xmlbeans.XmlCursor#toParent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GetCapabilitiesResponseEncoder.java From arctic-sea with Apache License 2.0 | 5 votes |
private void renameContentsElementNames(final Contents xbContents) { for (final Offering offering : xbContents.getContents().getOfferingArray()) { final XmlCursor cursor = offering.getAbstractOffering().newCursor(); cursor.setName(Sos2Constants.QN_OBSERVATION_OFFERING); cursor.removeAttribute(W3CConstants.QN_XSI_TYPE); if (cursor.toChild(Sos2Constants.QN_SOS_OBSERVED_AREA)) { cursor.setName(Sos2Constants.QN_SOS_OBSERVED_AREA); cursor.toParent(); } if (cursor.toChild(Sos2Constants.QN_SOS_PHENOMENON_TIME)) { cursor.setName(Sos2Constants.QN_SOS_PHENOMENON_TIME); cursor.toParent(); } if (cursor.toChild(Sos2Constants.QN_SOS_RESULT_TIME)) { cursor.setName(Sos2Constants.QN_SOS_RESULT_TIME); cursor.toParent(); } if (cursor.toChild(Sos2Constants.QN_SOS_RESPONSE_FORMAT)) { cursor.setName(Sos2Constants.QN_SOS_RESPONSE_FORMAT); while (cursor.toNextSibling(Sos2Constants.QN_SOS_RESPONSE_FORMAT)) { cursor.setName(Sos2Constants.QN_SOS_RESPONSE_FORMAT); } cursor.toParent(); } if (cursor.toChild(Sos2Constants.QN_SOS_OBSERVATION_TYPE)) { cursor.setName(Sos2Constants.QN_SOS_OBSERVATION_TYPE); while (cursor.toNextSibling(Sos2Constants.QN_SOS_OBSERVATION_TYPE)) { cursor.setName(Sos2Constants.QN_SOS_OBSERVATION_TYPE); } cursor.toParent(); } if (cursor.toChild(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE)) { cursor.setName(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE); while (cursor.toNextSibling(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE)) { cursor.setName(Sos2Constants.QN_SOS_FEATURE_OF_INTEREST_TYPE); } } cursor.dispose(); } }
Example 2
Source File: XmlBeanAssert.java From mdw with Apache License 2.0 | 5 votes |
/** * Uses cursors to compare two XML documents, ignoring whitespace and * ordering of attributes. Fails the JUnit test if they're different. * * @param message to display on test failure (may be null) * @param expected * @param actual */ public static void assertEquals(String message, XmlCursor expected, XmlCursor actual) { for (int child = 0; true; child++) { boolean child1 = expected.toChild(child); boolean child2 = actual.toChild(child); if (child1 != child2) { fail(message, "Different XML structure near " + QNameHelper.pretty(expected.getName())); } else if (expected.getName() != null && !expected.getName().equals(actual.getName())) { fail(message, "Expected element: '" + expected.getName() + "' differs from actual element: '" + actual.getName() + "'"); } else if (child == 0 && !child1) { if (!(expected.getTextValue().equals(actual.getTextValue()))) { fail(message, "Expected value for element " + QNameHelper.pretty(expected.getName()) + " -> '" + expected.getTextValue() + "' differs from actual value '" + actual.getTextValue() + "'"); } break; } else if (child1) { assertEquals(message, expected, actual); expected.toParent(); actual.toParent(); } else { break; } } assertAttributesEqual(message, expected, actual); }
Example 3
Source File: XML.java From astor with GNU General Public License v2.0 | 5 votes |
protected void changeNS (String oldURI, String newURI) { XmlCursor curs = newCursor(); while (curs.toParent()) { /* Goto the top of the document */ } TokenType tt = curs.currentTokenType(); if (tt.isStartdoc()) { tt = curs.toFirstContentToken(); } if (tt.isStart()) { do { if (tt.isStart() || tt.isAttr() || tt.isNamespace()) { javax.xml.namespace.QName currQName = curs.getName(); if (oldURI.equals(currQName.getNamespaceURI())) { curs.setName(new javax.xml.namespace.QName(newURI, currQName.getLocalPart())); } } tt = curs.toNextToken(); } while (!tt.isEnddoc() && !tt.isNone()); } curs.dispose(); }
Example 4
Source File: NamespaceHelper.java From astor with GNU General Public License v2.0 | 5 votes |
/** * @return List of Namespace objects that are declared in the container pointed to by the cursor. */ public static Object[] namespaceDeclarations(XMLLibImpl lib, XmlCursor cursor) { ObjArray declarations = new ObjArray(); NamespaceHelper helper = new NamespaceHelper(lib); cursor.push(); int depth = 0; while(cursor.hasPrevToken()) { if(cursor.isContainer()) { cursor.push(); depth++; } cursor.toParent(); } for(int i = 0; i < depth - 1; i++) { cursor.pop(); helper.update(cursor, null); } if(depth > 0) { cursor.pop(); helper.update(cursor, declarations); } cursor.pop(); return declarations.toArray(); }
Example 5
Source File: NamespaceHelper.java From astor with GNU General Public License v2.0 | 5 votes |
/** * @return Prefix to URI map of all namespaces in scope at the cursor. */ public static Map getAllNamespaces(XMLLibImpl lib, XmlCursor cursor) { NamespaceHelper helper = new NamespaceHelper(lib); cursor.push(); int depth = 0; while(cursor.hasPrevToken()) { if(cursor.isContainer()) { cursor.push(); depth++; } cursor.toParent(); } for(int i = 0; i < depth; i++) { cursor.pop(); helper.update(cursor, null); } cursor.pop(); return helper.prefixToURI; }
Example 6
Source File: XmlBeanAssert.java From mdw with Apache License 2.0 | 4 votes |
/** * Compares the attributes of the elements at the current position of two XmlCursors. * The ordering of the attributes is ignored in the comparison. Fails the JUnit test * case if the attributes or their values are different. * * @param message to display on test failure (may be null) * @param expected * @param actual */ private static void assertAttributesEqual(String message, XmlCursor expected, XmlCursor actual) { Map<QName,String> map1 = new HashMap<QName,String>(); Map<QName,String> map2 = new HashMap<QName,String>(); boolean attr1 = expected.toFirstAttribute(); boolean attr2 = actual.toFirstAttribute(); if (attr1 != attr2) { if (expected.isAttr() || actual.isAttr()) { expected.toParent(); } fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'"); } else if (!attr1) { return; } else { map1.put(expected.getName(), expected.getTextValue()); map2.put(actual.getName(), actual.getTextValue()); } while (true) { attr1 = expected.toNextAttribute(); attr2 = actual.toNextAttribute(); if (attr1 != attr2) { if (expected.isAttr() || actual.isAttr()) { expected.toParent(); } fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'"); } else if (!attr1) { break; } else { map1.put(expected.getName(), expected.getTextValue()); map2.put(actual.getName(), actual.getTextValue()); } } expected.toParent(); actual.toParent(); // check that attribute maps match, neglecting order Iterator<QName> iter = map1.keySet().iterator(); while (iter.hasNext()) { QName name = iter.next(); String value1 = map1.get(name); String value2 = map2.get(name); if (value2 == null) { fail(message, "Expected attribute value missing for element: " + QNameHelper.pretty(expected.getName()) + "--> '" + name + "'"); } else if (!value2.equals(value1)) { fail(message, "Attribute values for element '" + QNameHelper.pretty(expected.getName()) + "': Expected '" + value1 + "', Actual '" + value2 + "'"); } } }
Example 7
Source File: SoapMessageValidator.java From microcks with Apache License 2.0 | 4 votes |
/** * Validate a soap message accordingly to its WSDL and linked XSD resources. The validation is * done for a specified message part (maybe be the input, output or fault of an operation). * @param partName The name of the part to validate ie. name of the input, output or fault part (ex: sayHello) * @param partNamespace The namespace of the part to validate (ex: http://www.mma.fr/test/service) * @param message The full soap message as a string * @param wsdlUrl The URL where we can resolve service and operation WSDL * @param validateMessageBody Should we validate also the body ? If false, only Soap envelope is validated. * @return The list of validation failures. If empty, message is valid ! * @throws org.apache.xmlbeans.XmlException if given message is not a valid Xml document */ public static List<XmlError> validateSoapMessage(String partName, String partNamespace, String message, String wsdlUrl, boolean validateMessageBody) throws XmlException { // WsdlContext ctx = new WsdlContext(wsdlUrl); List<XmlError> errors = new ArrayList<XmlError>(); ctx.getSoapVersion().validateSoapEnvelope(message, errors); log.debug("SoapEnvelope validation errors: " + errors.size()); if (validateMessageBody){ // Create XmlBeans object for the soap message. XmlOptions xmlOptions = new XmlOptions(); xmlOptions.setLoadLineNumbers(); xmlOptions.setLoadLineNumbers( XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT ); XmlObject xml = XmlUtils.createXmlObject(message, xmlOptions); // Build the QName string of the part name. Ex: {http://www.github.com/lbroudoux/service}sayHello String fullPartName = "{" + partNamespace + "}" + partName; // Extract the corresponding part from soap body. XmlObject[] paths = xml.selectPath( "declare namespace env='" + ctx.getSoapVersion().getEnvelopeNamespace() + "';" + "declare namespace ns='" + partNamespace + "';" + "$this/env:Envelope/env:Body/ns:" + partName); SchemaGlobalElement elm; try { elm = ctx.getSchemaTypeLoader().findElement(QName.valueOf(fullPartName)); } catch (Exception e) { log.error("Exception while loading schema information for " + fullPartName, e); throw new XmlException("Exception while loading schema information for " + fullPartName, e); } if ( elm != null ){ validateMessageBody(ctx, errors, elm.getType(), paths[0]); // Ensure no other elements in body. NodeList children = XmlUtils.getChildElements((Element) paths[0].getDomNode().getParentNode()); for (int c = 0; c < children.getLength(); c++){ QName childName = XmlUtils.getQName(children.item(c)); // Compare child QName to full part QName. if (!fullPartName.equals(childName.toString())){ XmlCursor cur = paths[0].newCursor(); cur.toParent(); cur.toChild( childName ); errors.add( XmlError.forCursor( "Invalid element [" + childName + "] in SOAP Body", cur ) ); cur.dispose(); } } } log.debug("SoapBody validation errors: " + errors.size()); } return errors; }