/** * @param cursor * @return the inserted table */ public XWPFTable insertNewTbl(XmlCursor cursor) { if (isCursorInHdrF(cursor)) { String uri = CTTbl.type.getName().getNamespaceURI(); String localPart = "tbl"; cursor.beginElement(localPart, uri); cursor.toParent(); CTTbl t = (CTTbl) cursor.getObject(); XWPFTable newT = new XWPFTable(t, this); cursor.removeXmlContents(); XmlObject o = null; while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) { o = cursor.getObject(); } if (!(o instanceof CTTbl)) { tables.add(0, newT); } else { int pos = tables.indexOf(getTable((CTTbl) o)) + 1; tables.add(pos, newT); } int i = 0; cursor = t.newCursor(); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newT); cursor = t.newCursor(); cursor.toEndToken(); return newT; } return null; }
/** * add a new paragraph at position of the cursor * * @param cursor * @return the inserted paragraph */ public XWPFParagraph insertNewParagraph(XmlCursor cursor) { if (isCursorInHdrF(cursor)) { String uri = CTP.type.getName().getNamespaceURI(); String localPart = "p"; cursor.beginElement(localPart, uri); cursor.toParent(); CTP p = (CTP) cursor.getObject(); XWPFParagraph newP = new XWPFParagraph(p, this); XmlObject o = null; while (!(o instanceof CTP) && (cursor.toPrevSibling())) { o = cursor.getObject(); } if ((!(o instanceof CTP)) || (CTP) o == p) { paragraphs.add(0, newP); } else { int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1; paragraphs.add(pos, newP); } int i = 0; cursor.toCursor(p.newCursor()); while (cursor.toPrevSibling()) { o = cursor.getObject(); if (o instanceof CTP || o instanceof CTTbl) i++; } bodyElements.add(i, newP); cursor.toCursor(p.newCursor()); cursor.toEndToken(); return newP; } return null; }
/** * Utility method to append the contents of the child docment to the end of the parent XmlObject. * This is useful when dealing with elements without generated methods (like elements with xs:any) * * @param parent Parent to append contents to * @param childDoc Xml document containing contents to be appended */ public static void append(final XmlObject parent, final XmlObject childDoc) { final XmlCursor parentCursor = parent.newCursor(); parentCursor.toEndToken(); final XmlCursor childCursor = childDoc.newCursor(); childCursor.toFirstChild(); childCursor.moveXml(parentCursor); parentCursor.dispose(); childCursor.dispose(); }
/** * Uses the XPath text() function to get values from <name> elements in received XML, then * collects those values as the value of a <names> element created here. * * <p>Demonstrates the following characteristics of the selectPath method: * * <p>- It supports expressions that include XPath function calls. - selectPath called from an * XmlCursor instance (instead of an XMLBeans type) places results (if any) into the cursor's * selection set. * * @param empDoc The incoming XML. * @return <code>true</code> if the XPath expression returned results; otherwise, <code>false * </code>. */ public boolean collectNames(XmlObject empDoc) { boolean hasResults = false; // Create a cursor with which to execute query expressions. The cursor // is inserted at the very beginning of the incoming XML, then moved to // the first element's START token. XmlCursor pathCursor = empDoc.newCursor(); pathCursor.toFirstChild(); // Execute the path expression, qualifying it with the namespace // declaration. pathCursor.selectPath(m_namespaceDeclaration + "$this//xq:employee/xq:name/text()"); // If there are results, then go ahead and do stuff. if (pathCursor.getSelectionCount() > 0) { hasResults = true; // Create a new <names> element into which names from the XML // will be copied. Note that this element is in the default // namespace; it's not part of the schema. XmlObject namesElement = null; try { namesElement = XmlObject.Factory.parse("<names/>"); } catch (XmlException e) { e.printStackTrace(); } // Add a cursor the new element and put it between its START and END // tokens, where new values can be inserted. XmlCursor namesCursor = namesElement.newCursor(); namesCursor.toFirstContentToken(); namesCursor.toEndToken(); // Loop through the selections, appending the incoming <name> element's // value to the new <name> element's value. (Of course, this could have // been done with a StringBuffer, but that wouldn't show the cursor in // use.) while (pathCursor.toNextSelection()) { namesCursor.insertChars(pathCursor.getTextValue()); if (pathCursor.hasNextSelection()) { namesCursor.insertChars(", "); } } // Dispose of the cursors now that they're not needed. pathCursor.dispose(); namesCursor.dispose(); // Print the new element. System.out.println("\nNames collected by collectNames method: \n\n" + namesElement + "\n"); } return hasResults; }
private x0.scimSchemasCore1.Resource internalEncode( Resource resource, List<String> attributesList) { try { Object xmlObject = createXmlObject(resource); x0.scimSchemasCore1.Resource xmlResource = (x0.scimSchemasCore1.Resource) new ComplexHandler().encodeXml(resource, attributesList, null, xmlObject); // TODO check include attributes for extensions too List<Object> extensions = resource.getExtensions(); for (Object extension : extensions) { if (!extension.getClass().isAnnotationPresent(Extension.class)) { throw new RuntimeException( "The extension '" + extension.getClass().getName() + "' has no namespace, try to add Extension annotation to class"); } Extension extensionMetaData = extension.getClass().getAnnotation(Extension.class); for (Method method : extension.getClass().getMethods()) { if (!method.isAnnotationPresent(Attribute.class)) { continue; } Object data = method.invoke(extension); if (data == null) { continue; } MetaData metaData = new MetaData(method.getAnnotation(Attribute.class)); String attributeName = extensionMetaData.schema() + "." + metaData.getName(); if (attributesList != null && !attributesList.contains(attributeName)) { continue; } Class<?> factory = ReflectionHelper.getFactory(metaData.getXmlDoc()); XmlObject doc = (XmlObject) factory.getMethod("newInstance").invoke(null); Object innerXml = null; try { String adder = "addNew"; adder += metaData.getName().substring(0, 1).toUpperCase(); adder += metaData.getName().substring(1); innerXml = doc.getClass().getMethod(adder).invoke(doc); } catch (NoSuchMethodException e) { // It is okay this is a simple type } IEncodeHandler encoder = metaData.getEncoder(); Object result = encoder.encodeXml(data, attributesList, metaData.getInternalMetaData(), innerXml); String setter = "set"; setter += metaData.getName().substring(0, 1).toUpperCase(); setter += metaData.getName().substring(1); ReflectionHelper.getMethod(setter, doc.getClass()).invoke(doc, result); XmlCursor docCursor = doc.newCursor(); docCursor.toFirstChild(); XmlCursor cursor = xmlResource.newCursor(); cursor.toEndToken(); docCursor.moveXml(cursor); cursor.dispose(); docCursor.dispose(); } } return xmlResource; } catch (SecurityException e) { throw new RuntimeException("Internal error, encoding xml", e); } catch (IllegalAccessException e) { throw new RuntimeException("Internal error, encoding xml", e); } catch (IllegalArgumentException e) { throw new RuntimeException("Internal error, encoding xml", e); } catch (InvocationTargetException e) { throw new RuntimeException("Internal error, encoding xml", e); } catch (FactoryNotFoundException e) { throw new RuntimeException("Internal error, encoding xml", e); } catch (NoSuchMethodException e) { throw new RuntimeException("Internal error, encoding xml", e); } }