List of usage examples for org.dom4j Element createCopy
Element createCopy();
From source file:org.onosproject.yang.serializers.xml.DefaultXmlWalker.java
License:Apache License
/** * Determine the type of an element.//ww w.j a v a 2 s . co m * * @param element to be analysed * @return type of the element */ private XmlNodeType getElementType(Element element) { Element newElement = element.createCopy(); newElement.remove(element.getNamespace()); return newElement.hasContent() && newElement.isTextOnly() ? TEXT_NODE : OBJECT_NODE; }
From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.DefaultXmlCodecWalker.java
License:Apache License
@Override public void walk(XmlListener listener, Element element, Element rootElement) { try {/* w w w.j a v a 2s . c o m*/ Element newElement = element.createCopy(); newElement.remove(element.getNamespace()); listener.enterXmlElement(element, getElementType(newElement), rootElement); if (element.hasContent() && !element.isTextOnly()) { for (Iterator i = element.elementIterator(); i.hasNext();) { Element childElement = (Element) i.next(); walk(listener, childElement, rootElement); } } listener.exitXmlElement(element, getElementType(element), rootElement); } catch (Exception e) { log.error("Exception occurred when walk xml element: {}", element); } }
From source file:org.opencms.xml.A_CmsXmlDocument.java
License:Open Source License
/** * @see org.opencms.xml.I_CmsXmlDocument#copyLocale(java.util.Locale, java.util.Locale) *//*w w w . j a v a 2 s . c om*/ public void copyLocale(Locale source, Locale destination) throws CmsXmlException { if (!hasLocale(source)) { throw new CmsXmlException(Messages.get().container(Messages.ERR_LOCALE_NOT_AVAILABLE_1, source)); } if (hasLocale(destination)) { throw new CmsXmlException(Messages.get().container(Messages.ERR_LOCALE_ALREADY_EXISTS_1, destination)); } Element sourceElement = null; Element rootNode = m_document.getRootElement(); Iterator<Element> i = CmsXmlGenericWrapper.elementIterator(rootNode); String localeStr = source.toString(); while (i.hasNext()) { Element element = i.next(); String language = element.attributeValue(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE, null); if ((language != null) && (localeStr.equals(language))) { // detach node with the locale sourceElement = element.createCopy(); // there can be only one node for the locale break; } } if (sourceElement == null) { // should not happen since this was checked already, just to make sure... throw new CmsXmlException(Messages.get().container(Messages.ERR_LOCALE_NOT_AVAILABLE_1, source)); } // switch locale value in attribute of copied node sourceElement.addAttribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE, destination.toString()); // attach the copied node to the root node rootNode.add(sourceElement); // re-initialize the document bookmarks initDocument(m_document, m_encoding, getContentDefinition()); }
From source file:org.opencms.xml.A_CmsXmlDocument.java
License:Open Source License
/** * Creates a partial deep element copy according to the set of element paths.<p> * Only elements contained in that set will be copied. * /*from ww w . j av a2 s . c o m*/ * @param parentPath the path of the parent element or <code>null</code>, initially * @param parent the parent element * @param element the element to copy * @param copyElements the set of paths for elements to copy * * @return a partial deep copy of <code>element</code> */ private Element createDeepElementCopyInternal(String parentPath, Element parent, Element element, Set<String> copyElements) { String elName = element.getName(); if (parentPath != null) { Element first = element.getParent().element(elName); int elIndex = (element.getParent().indexOf(element) - first.getParent().indexOf(first)) + 1; elName = parentPath + (parentPath.length() > 0 ? "/" : "") + elName.concat("[" + elIndex + "]"); } if ((parentPath == null) || copyElements.contains(elName)) { // this is a content element we want to copy Element copy = element.createCopy(); // copy.detach(); if (parentPath != null) { parent.add(copy); } // check if we need to copy subelements, too boolean copyNested = (parentPath == null); for (Iterator<String> i = copyElements.iterator(); !copyNested && i.hasNext();) { String path = i.next(); copyNested = !elName.equals(path) && path.startsWith(elName); } if (copyNested) { copy.clearContent(); for (Iterator<Element> i = CmsXmlGenericWrapper.elementIterator(element); i.hasNext();) { Element el = i.next(); createDeepElementCopyInternal((parentPath == null) ? "" : elName, copy, el, copyElements); } } return copy; } else { return null; } }
From source file:org.opencms.xml.page.CmsXmlPage.java
License:Open Source License
/** * Converts the XML structure of the pre 5.5.0 development version of * the XML page to the final 6.0 version.<p> *//* ww w . j av a 2s .c o m*/ private void convertOldDocument() { Document newDocument = DocumentHelper.createDocument(); Element root = newDocument.addElement(NODE_PAGES); root.add(I_CmsXmlSchemaType.XSI_NAMESPACE); root.addAttribute(I_CmsXmlSchemaType.XSI_NAMESPACE_ATTRIBUTE_NO_SCHEMA_LOCATION, XMLPAGE_XSD_SYSTEM_ID); Map<String, Element> pages = new HashMap<String, Element>(); if ((m_document.getRootElement() != null) && (m_document.getRootElement().element(NODE_ELEMENTS) != null)) { for (Iterator<Element> i = CmsXmlGenericWrapper.elementIterator( m_document.getRootElement().element(NODE_ELEMENTS), NODE_ELEMENT); i.hasNext();) { Element elem = i.next(); try { String elementName = elem.attributeValue(ATTRIBUTE_NAME); String elementLang = elem.attributeValue(ATTRIBUTE_LANGUAGE); String elementEnabled = elem.attributeValue(ATTRIBUTE_ENABLED); boolean enabled = (elementEnabled == null) ? true : Boolean.valueOf(elementEnabled).booleanValue(); Element page = pages.get(elementLang); if (page == null) { // no page available for the language, add one page = root.addElement(NODE_PAGE).addAttribute(ATTRIBUTE_LANGUAGE, elementLang); pages.put(elementLang, page); } Element newElement = page.addElement(NODE_ELEMENT).addAttribute(ATTRIBUTE_NAME, elementName); if (!enabled) { newElement.addAttribute(ATTRIBUTE_ENABLED, String.valueOf(enabled)); } Element links = elem.element(NODE_LINKS); if (links != null) { newElement.add(links.createCopy()); } Element content = elem.element(NODE_CONTENT); if (content != null) { newElement.add(content.createCopy()); } } catch (NullPointerException e) { LOG.error(Messages.get().getBundle().key(Messages.ERR_XML_PAGE_CONVERT_CONTENT_0), e); } } } // now replace the old with the new document m_document = newDocument; }
From source file:org.orbeon.oxf.processor.generator.DOMGenerator.java
License:Open Source License
private static org.dom4j.Document makeCopyDoc(final org.dom4j.Element e) { final org.dom4j.Element cpy = e.createCopy(); final NonLazyUserDataDocument ret = new NonLazyUserDataDocument(); ret.setRootElement(cpy);//w w w. ja va 2 s. c om return ret; }
From source file:org.orbeon.oxf.processor.ImageServer.java
License:Open Source License
private String computeCacheFileName(String type, String path, List<Element> nodes) { // Create digest document and digest Document document = new NonLazyUserDataDocument(); Element rootElement = document.addElement("image"); for (Element element : nodes) { rootElement.add(element.createCopy()); }/*w w w .j ava2 s.c o m*/ String digest = NumberUtils.toHexString(Dom4jUtils.getDigest(document)); // Create file name if ("flat".equals(type)) return computePathNameFlat(path) + "-" + digest; else return computePathNameHierarchical(path) + "-" + digest; }
From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java
License:Open Source License
/** * Return a new document with a copy of newRoot as its root. *///from w w w .j a v a 2 s . c o m public static Document createDocumentCopyElement(final Element newRoot) { final Element copy = newRoot.createCopy(); final DocumentFactory factory = NonLazyUserDataDocumentFactory.getInstance(); return factory.createDocument(copy); }
From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java
License:Open Source License
/** * Return a copy of the given element which includes all the namespaces in scope on the element. * * @param sourceElement element to copy//www. j a v a2 s .c o m * @return copied element */ public static Element copyElementCopyParentNamespaces(final Element sourceElement) { final Element newElement = sourceElement.createCopy(); copyMissingNamespaces(sourceElement.getParent(), newElement); return newElement; }
From source file:org.sapia.util.xml.confix.Dom4jProcessor.java
License:Open Source License
private Object process(Object aParent, Element anElement, String setterName) throws ProcessingException { String aName = anElement.getName(); if (setterName == null) { setterName = aName;//from www . j a va2 s . c o m } CreationStatus status = null; try { status = getObjectFactory().newObjectFor(anElement.getNamespace().getPrefix(), anElement.getNamespace().getURI(), aName, aParent); } catch (ObjectCreationException oce) { if (aParent == null) { String aMessage = "Unable to create an object for the element " + anElement; throw new ProcessingException(aMessage, oce); } List children; if ((aParent != null) && (containsMethod("set", aParent, aName) || containsMethod("add", aParent, aName)) && ((children = anElement.elements()).size() == 1)) { Element child = (Element) children.get(0); process(aParent, child, setterName); return aParent; } try { String aValue = anElement.getTextTrim(); invokeSetter(aParent.getClass().getName(), aParent, aName, aValue); return aParent; } catch (ConfigurationException ce) { String aMessage = "Unable to create an object nor to call a setter for the element " + anElement; oce.printStackTrace(); throw new ProcessingException(aMessage, ce); } } String text = anElement.getTextTrim(); if (text.length() > 0) { try { invokeSetter(aName, status.getCreated(), "Text", text); } catch (ConfigurationException ce) { String aMessage = "The object '" + aName + "' does not accept free text"; throw new ProcessingException(aMessage, ce); } } try { // Process the attributes of the DOM element for (Iterator it = anElement.attributeIterator(); it.hasNext();) { Attribute attr = (Attribute) it.next(); invokeSetter(aName, status.getCreated(), attr.getName(), attr.getValue()); } // Process the child elements for (Iterator it = anElement.elementIterator(); it.hasNext();) { Element child = (Element) it.next(); if (status.getCreated() instanceof Dom4jHandlerIF) { ((Dom4jHandlerIF) status.getCreated()).handleElement(child); } else if (status.getCreated() instanceof XMLConsumer) { XMLConsumer cons = (XMLConsumer) status.getCreated(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLWriter writer; try { writer = new XMLWriter(bos, OutputFormat.createPrettyPrint()); } catch (UnsupportedEncodingException e) { throw new ProcessingException("Could not instantiate XMLWriter", e); } try { Element copy = child.createCopy(); copy.setDocument(null); writer.write(DocumentHelper.createDocument(copy)); ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray()); InputSource is = new InputSource(in); cons.consume(is); } catch (Exception e) { throw new ProcessingException("Could not pipe content of element: " + child.getQualifiedName() + " to XMLConsumer", e); } } else { process(status.getCreated(), child); } } // before assigning to parent, check if object // implements ObjectCreationCallback. if (status.getCreated() instanceof ObjectCreationCallback) { status._created = ((ObjectCreationCallback) status.getCreated()).onCreate(); } // assign obj to parent through setXXX or addXXX if ((aParent != null) && !status.wasAssigned() && !(status.getCreated() instanceof NullObject)) { assignToParent(aParent, status.getCreated(), setterName); } if (status.getCreated() instanceof NullObject) { return null; } return status.getCreated(); } catch (ConfigurationException ce) { String aMessage = "Unable to process the content of the element " + aName; throw new ProcessingException(aMessage, ce); } }