List of usage examples for org.dom4j Attribute getQName
QName getQName();
QName
of this attribute which represents the local name, the qualified name and the Namespace
. From source file:com.rdvmedecin.proprietes.Properties.java
public Properties() throws DocumentException { File fichier = new File("configuration.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(fichier); Element root = doc.getRootElement(); List attributes = root.attributes(); List elements = root.elements(); QName qName = root.getQName(); String nom = qName.getName(); // lecture du nom de l'espace de noms String nomEspaceDeNoms = qName.getNamespaceURI(); // lecture du prfixe utilis pour cet espace de nom String nomPrefix = qName.getNamespacePrefix(); String texte = root.getText(); Attribute attribute = (Attribute) attributes.iterator().next(); QName attributeQName = attribute.getQName(); String value = attribute.getValue(); String nomAttribut = attribute.getName(); }
From source file:org.alfresco.module.vti.web.ws.GetListEndpoint.java
License:Open Source License
@SuppressWarnings("unchecked") private void copyElement(Element source, Element target) { Element copy = target.addElement(source.getQName()); for (Attribute attr : (List<Attribute>) source.attributes()) { copy.addAttribute(attr.getQName(), attr.getValue()); }//from w w w . ja v a 2 s . c o m for (Element child : (List<Element>) source.elements()) { copyElement(child, copy); } if (source.getText() != null) { copy.setText(source.getText()); } }
From source file:org.apereo.portal.layout.dlm.RDBMDistributedLayoutStore.java
License:Apache License
private org.dom4j.Element getExportLayoutDom(IPerson person, IUserProfile profile) { if (!this.layoutExistsForUser(person)) { return null; }/*from w ww. ja va 2 s . c om*/ org.dom4j.Document layoutDoc = null; try { final Document layoutDom = this._safeGetUserLayout(person, profile); person.setAttribute(Constants.PLF, layoutDom); layoutDoc = this.reader.get().read(layoutDom); } catch (final Throwable t) { final String msg = "Unable to obtain layout & profile for user '" + person.getUserName() + "', profileId " + profile.getProfileId(); throw new RuntimeException(msg, t); } if (logger.isDebugEnabled()) { // Write out this version of the layout to the log for dev purposes... final StringWriter str = new StringWriter(); final XMLWriter xml = new XMLWriter(str, new OutputFormat(" ", true)); try { xml.write(layoutDoc); xml.close(); } catch (final Throwable t) { throw new RuntimeException( "Failed to write the layout for user '" + person.getUserName() + "' to the DEBUG log", t); } logger.debug("Layout for user: {}\n{}", person.getUserName(), str.getBuffer().toString()); } /* * Attempt to detect a corrupted layout; return null in such cases */ if (isLayoutCorrupt(layoutDoc)) { logger.warn("Layout for user: {} is corrupt; layout structures will not be exported.", person.getUserName()); return null; } /* * Clean up the DOM for export. */ // (1) Add structure & theme attributes... final int structureStylesheetId = profile.getStructureStylesheetId(); this.addStylesheetUserPreferencesAttributes(person, profile, layoutDoc, structureStylesheetId, "structure"); final int themeStylesheetId = profile.getThemeStylesheetId(); this.addStylesheetUserPreferencesAttributes(person, profile, layoutDoc, themeStylesheetId, "theme"); // (2) Remove locale info... final Iterator<org.dom4j.Attribute> locale = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@locale").iterator(); while (locale.hasNext()) { final org.dom4j.Attribute loc = locale.next(); loc.getParent().remove(loc); } // (3) Scrub unnecessary channel information... for (final Iterator<org.dom4j.Element> orphanedChannels = (Iterator<org.dom4j.Element>) layoutDoc .selectNodes("//channel[@fname = '']").iterator(); orphanedChannels.hasNext();) { // These elements represent UP_LAYOUT_STRUCT rows where the // CHAN_ID field was not recognized by ChannelRegistryStore; // best thing to do is remove the elements... final org.dom4j.Element ch = orphanedChannels.next(); ch.getParent().remove(ch); } final List<String> channelAttributeWhitelist = Arrays.asList(new String[] { "fname", "unremovable", "hidden", "immutable", "ID", "dlm:plfID", "dlm:moveAllowed", "dlm:deleteAllowed" }); final Iterator<org.dom4j.Element> channels = (Iterator<org.dom4j.Element>) layoutDoc .selectNodes("//channel").iterator(); while (channels.hasNext()) { final org.dom4j.Element oldCh = channels.next(); final org.dom4j.Element parent = oldCh.getParent(); final org.dom4j.Element newCh = this.fac.createElement("channel"); for (final String aName : channelAttributeWhitelist) { final org.dom4j.Attribute a = (org.dom4j.Attribute) oldCh.selectSingleNode("@" + aName); if (a != null) { newCh.addAttribute(a.getQName(), a.getValue()); } } parent.elements().add(parent.elements().indexOf(oldCh), newCh); parent.remove(oldCh); } // (4) Convert internal DLM noderefs to external form (pathrefs)... for (final Iterator<org.dom4j.Attribute> origins = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:origin").iterator(); origins.hasNext();) { final org.dom4j.Attribute org = origins.next(); final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), org.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... org.setValue(dlmPathref.toString()); } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", org.getUniquePath(), person.getAttribute(IPerson.USERNAME), org.getValue()); } } } for (final Iterator<org.dom4j.Attribute> it = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:target").iterator(); it.hasNext();) { final org.dom4j.Attribute target = it.next(); final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), target.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... target.setValue(dlmPathref.toString()); } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", target.getUniquePath(), person.getAttribute(IPerson.USERNAME), target.getValue()); } } } for (final Iterator<org.dom4j.Attribute> names = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//dlm:*/@name").iterator(); names.hasNext();) { final org.dom4j.Attribute n = names.next(); if (n.getValue() == null || n.getValue().trim().length() == 0) { // Outer <dlm:positionSet> elements don't seem to use the name // attribute, though their childern do. Just skip these so we // don't send a false WARNING. continue; } final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), n.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... n.setValue(dlmPathref.toString()); // These *may* have fnames... if (dlmPathref.getPortletFname() != null) { n.getParent().addAttribute("fname", dlmPathref.getPortletFname()); } } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", n.getUniquePath(), person.getAttribute(IPerson.USERNAME), n.getValue()); } } } // Remove synthetic Ids, but from non-fragment owners only... if (!this.isFragmentOwner(person)) { /* * In the case of fragment owners, the original database Ids allow * us keep (not break) the associations that subscribers have with * nodes on the fragment layout. */ // (5) Remove dlm:plfID... for (final Iterator<org.dom4j.Attribute> plfid = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:plfID").iterator(); plfid.hasNext();) { final org.dom4j.Attribute plf = plfid.next(); plf.getParent().remove(plf); } // (6) Remove database Ids... for (final Iterator<org.dom4j.Attribute> ids = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@ID").iterator(); ids.hasNext();) { final org.dom4j.Attribute a = ids.next(); a.getParent().remove(a); } } return layoutDoc.getRootElement(); }
From source file:org.craftercms.core.xml.mergers.impl.cues.impl.MergeCueResolverImpl.java
License:Open Source License
@Override public MergeCueContext getMergeCue(Element parent, Element child) { MergeCue parentMergeCue;// w ww .j ava 2s .co m MergeCue childMergeCue; Attribute parentMergeCueAttribute = getMergeCueAttribute(parent, parentMergeCues); if (parentMergeCueAttribute != null) { parentMergeCue = parentMergeCues.get(parentMergeCueAttribute.getQName()); } else { parentMergeCue = defaultParentMergeCue; } Attribute childMergeCueAttribute = getMergeCueAttribute(child, childMergeCues); if (childMergeCueAttribute != null) { childMergeCue = childMergeCues.get(childMergeCueAttribute.getQName()); } else { childMergeCue = defaultChildMergeCue; } MergeCue chosenMergeCue; Map<String, String> mergeCueParams; if (parentMergeCue.getPriority() > childMergeCue.getPriority()) { chosenMergeCue = parentMergeCue; if (parentMergeCueAttribute != null) { mergeCueParams = getMergeCueParams(parent, parentMergeCueAttribute); } else { mergeCueParams = Collections.emptyMap(); } } else { chosenMergeCue = childMergeCue; if (childMergeCueAttribute != null) { mergeCueParams = getMergeCueParams(child, childMergeCueAttribute); } else { mergeCueParams = Collections.emptyMap(); } } return new MergeCueContext(chosenMergeCue, parent, child, mergeCueParams); }
From source file:org.craftercms.core.xml.mergers.impl.cues.impl.MergeCueResolverImpl.java
License:Open Source License
@SuppressWarnings("unchecked") protected Attribute getMergeCueAttribute(Element element, Map<QName, MergeCue> mergeCues) { List<Attribute> attributes = element.attributes(); for (Iterator<Attribute> i = attributes.iterator(); i.hasNext();) { Attribute attribute = i.next(); if (mergeCues.containsKey(attribute.getQName())) { i.remove();// w ww. j a v a2s .c o m return attribute; } } return null; }
From source file:org.jasig.portal.layout.dlm.RDBMDistributedLayoutStore.java
License:Apache License
private org.dom4j.Element getExportLayoutDom(IPerson person, IUserProfile profile) { if (!this.layoutExistsForUser(person)) { return null; }/* ww w. j a v a2 s. co m*/ org.dom4j.Document layoutDoc = null; try { final Document layoutDom = this._safeGetUserLayout(person, profile); person.setAttribute(Constants.PLF, layoutDom); layoutDoc = this.reader.get().read(layoutDom); } catch (final Throwable t) { final String msg = "Unable to obtain layout & profile for user '" + person.getUserName() + "', profileId " + profile.getProfileId(); throw new RuntimeException(msg, t); } if (logger.isDebugEnabled()) { // Write out this version of the layout to the log for dev purposes... final StringWriter str = new StringWriter(); final XMLWriter xml = new XMLWriter(str, new OutputFormat(" ", true)); try { xml.write(layoutDoc); xml.close(); } catch (final Throwable t) { throw new RuntimeException( "Failed to write the layout for user '" + person.getUserName() + "' to the DEBUG log", t); } logger.debug("Layout for user: {}\n{}", person.getUserName(), str.getBuffer().toString()); } /* * Attempt to detect a corrupted layout; return null in such cases */ if (isLayoutCorrupt(layoutDoc)) { logger.warn("Layout for user: {} is corrupt; layout structures will not be exported.", person.getUserName()); return null; } /* * Clean up the DOM for export. */ // (1) Add structure & theme attributes... final int structureStylesheetId = profile.getStructureStylesheetId(); this.addStylesheetUserPreferencesAttributes(person, profile, layoutDoc, structureStylesheetId, "structure"); final int themeStylesheetId = profile.getThemeStylesheetId(); this.addStylesheetUserPreferencesAttributes(person, profile, layoutDoc, themeStylesheetId, "theme"); // (2) Remove locale info... final Iterator<org.dom4j.Attribute> locale = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@locale").iterator(); while (locale.hasNext()) { final org.dom4j.Attribute loc = locale.next(); loc.getParent().remove(loc); } // (3) Scrub unnecessary channel information... for (final Iterator<org.dom4j.Element> orphanedChannels = (Iterator<org.dom4j.Element>) layoutDoc .selectNodes("//channel[@fname = '']").iterator(); orphanedChannels.hasNext();) { // These elements represent UP_LAYOUT_STRUCT rows where the // CHAN_ID field was not recognized by ChannelRegistryStore; // best thing to do is remove the elements... final org.dom4j.Element ch = orphanedChannels.next(); ch.getParent().remove(ch); } final List<String> channelAttributeWhitelist = Arrays.asList(new String[] { "fname", "unremovable", "hidden", "immutable", "ID", "dlm:plfID", "dlm:moveAllowed", "dlm:deleteAllowed" }); final Iterator<org.dom4j.Element> channels = (Iterator<org.dom4j.Element>) layoutDoc .selectNodes("//channel").iterator(); while (channels.hasNext()) { final org.dom4j.Element oldCh = channels.next(); final org.dom4j.Element parent = oldCh.getParent(); final org.dom4j.Element newCh = this.fac.createElement("channel"); for (final String aName : channelAttributeWhitelist) { final org.dom4j.Attribute a = (org.dom4j.Attribute) oldCh.selectSingleNode("@" + aName); if (a != null) { newCh.addAttribute(a.getQName(), a.getValue()); } } parent.elements().add(parent.elements().indexOf(oldCh), newCh); parent.remove(oldCh); } // (4) Convert internal DLM noderefs to external form (pathrefs)... for (final Iterator<org.dom4j.Attribute> origins = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:origin").iterator(); origins.hasNext();) { final org.dom4j.Attribute org = origins.next(); final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), org.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... org.setValue(dlmPathref.toString()); } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", org.getUniquePath(), person.getAttribute(IPerson.USERNAME), org.getValue()); } } } for (final Iterator<org.dom4j.Attribute> it = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:target").iterator(); it.hasNext();) { final org.dom4j.Attribute target = it.next(); final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), target.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... target.setValue(dlmPathref.toString()); } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", target.getUniquePath(), person.getAttribute(IPerson.USERNAME), target.getValue()); } } } for (final Iterator<org.dom4j.Attribute> names = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//dlm:*/@name").iterator(); names.hasNext();) { final org.dom4j.Attribute n = names.next(); if (n.getValue() == null || n.getValue().trim().length() == 0) { // Outer <dlm:positionSet> elements don't seem to use the name // attribute, though their childern do. Just skip these so we // don't send a false WARNING. continue; } final Pathref dlmPathref = this.nodeReferenceFactory.getPathrefFromNoderef( (String) person.getAttribute(IPerson.USERNAME), n.getValue(), layoutDoc.getRootElement()); if (dlmPathref != null) { // Change the value only if we have a valid pathref... n.setValue(dlmPathref.toString()); // These *may* have fnames... if (dlmPathref.getPortletFname() != null) { n.getParent().addAttribute("fname", dlmPathref.getPortletFname()); } } else { if (logger.isWarnEnabled()) { logger.warn("Layout element '{}' from user '{}' failed to match noderef '{}'", n.getUniquePath(), person.getAttribute(IPerson.USERNAME), n.getValue()); } } } // Remove synthetic Ids, but from non-fragment owners only... if (!this.isFragmentOwner(person)) { /* * In the case of fragment owners, the original database Ids allow * us keep (not break) the associations that subscribers have with * nodes on the fragment layout. */ // (5) Remove dlm:plfID... for (final Iterator<org.dom4j.Attribute> plfid = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@dlm:plfID").iterator(); plfid.hasNext();) { final org.dom4j.Attribute plf = plfid.next(); plf.getParent().remove(plf); } // (6) Remove database Ids... for (final Iterator<org.dom4j.Attribute> ids = (Iterator<org.dom4j.Attribute>) layoutDoc .selectNodes("//@ID").iterator(); ids.hasNext();) { final org.dom4j.Attribute a = ids.next(); a.getParent().remove(a); } } return layoutDoc.getRootElement(); }
From source file:org.jivesoftware.openfire.plugin.Xep227Exporter.java
License:Apache License
/** * Adding offline messages, if there are some. * /*from w w w .j a v a 2 s.c o m*/ * @param hostname * host name * @param userElement * DOM element * @param userName * user name */ @SuppressWarnings("unchecked") private void exportOfflineMessages(String hostname, Element userElement, String userName) { Collection<OfflineMessage> offlineMessages = offlineMessagesStore.getMessages(userName, false); if (!offlineMessages.isEmpty()) { Element offlineElement = userElement.addElement(OFFLINE_MESSAGES_ELEMENT_NAME); for (OfflineMessage offMessage : offlineMessages) { Element messageElement = offlineElement.addElement(new QName(MESSAGE_ELEMENT_NAME, JABBER_MSG_NS)); for (Object att : offMessage.getElement().attributes()) { Attribute attribute = (Attribute) att; messageElement.addAttribute(attribute.getQName(), attribute.getValue()); } for (Iterator<Element> iterator = offMessage.getElement().elementIterator(); iterator.hasNext();) { Element element = iterator.next(); messageElement.add(element.createCopy(new QName(element.getName(), (element.getNamespace() == Namespace.NO_NAMESPACE ? JABBER_MSG_NS : element.getNamespace())))); } /** * Adding delay element */ Element delayElement = messageElement.addElement("delay", "urn:xmpp:delay"); delayElement.addAttribute(FROM_NAME, hostname); delayElement.addAttribute("stamp", XMPPDateTimeFormat.format(offMessage.getCreationDate())); delayElement.addText("Offline Storage"); } } }
From source file:org.orbeon.oxf.xforms.action.actions.XFormsInsertAction.java
License:Open Source License
private static List<Node> doInsert(Node insertionNode, List<Node> clonedNodes, XFormsInstance modifiedInstance, boolean doDispatch) { final List<Node> insertedNodes = new ArrayList<Node>(clonedNodes.size()); if (insertionNode instanceof Element) { // Insert inside an element final Element insertContextElement = (Element) insertionNode; int otherNodeIndex = 0; for (Node clonedNode : clonedNodes) { if (clonedNode != null) {// NOTE: we allow passing some null nodes so we check on null if (clonedNode instanceof Attribute) { // Add attribute to element // NOTE: In XML, attributes are unordered. dom4j handles them as a list so has order, but the // XForms spec shouldn't rely on attribute order. We could try to keep the order, but it is harder // as we have to deal with removing duplicate attributes and find a reasonable insertion strategy. final Attribute clonedAttribute = (Attribute) clonedNode; final Attribute existingAttribute = insertContextElement .attribute(clonedAttribute.getQName()); if (existingAttribute != null) insertContextElement.remove(existingAttribute); insertContextElement.add(clonedAttribute); if (existingAttribute != null) { // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance .documentInfo(); Dispatch.dispatchEvent(new XXFormsReplaceEvent(modifiedInstance, documentWrapper.wrap(existingAttribute), documentWrapper.wrap(clonedAttribute))); }/*from w ww . j a va 2s .c om*/ } insertedNodes.add(clonedAttribute); } else if (!(clonedNode instanceof Document)) { // Add other node to element Dom4jUtils.content(insertContextElement).add(otherNodeIndex++, clonedNode); insertedNodes.add(clonedNode); } else { // "If a cloned node cannot be placed at the target location due to a node type conflict, then the // insertion for that particular clone node is ignored." } } } return insertedNodes; } else if (insertionNode instanceof Document) { final Document insertContextDocument = (Document) insertionNode; // "If there is more than one cloned node to insert, only the first node that does not cause a conflict is // considered." for (Node clonedNode : clonedNodes) { // Only an element can be inserted at the root of an instance if (clonedNode instanceof Element) { final Element formerRootElement = insertContextDocument.getRootElement(); insertContextDocument.setRootElement((Element) clonedNode); // Dispatch xxforms-replace event if required and possible // NOTE: For now, still dispatch xforms-insert for backward compatibility. if (doDispatch && modifiedInstance != null) { final DocumentWrapper documentWrapper = (DocumentWrapper) modifiedInstance.documentInfo(); Dispatch.dispatchEvent( new XXFormsReplaceEvent(modifiedInstance, documentWrapper.wrap(formerRootElement), documentWrapper.wrap(insertContextDocument.getRootElement()))); } insertedNodes.add(clonedNode); return insertedNodes; } } // NOTE: The spec does not allow inserting comments and PIs at the root of an instance document at this // point. return insertedNodes; } else { throw new OXFException("Unsupported insertion node type: " + insertionNode.getClass().getName()); } }
From source file:org.orbeon.oxf.xforms.xbl.XBLTransformer.java
License:Open Source License
/** * Apply an XBL transformation, i.e. apply xbl:content, xbl:attr, etc. * * NOTE: This mutates shadowTreeDocument. *///from w ww.j a va 2 s . c o m public static Document transform(final Document shadowTreeDocument, final Element boundElement, final boolean excludeNestedHandlers, final boolean excludeNestedLHHA) { final DocumentWrapper documentWrapper = new DocumentWrapper(boundElement.getDocument(), null, XPathCache.getGlobalConfiguration()); Dom4jUtils.visitSubtree(shadowTreeDocument.getRootElement(), new Dom4jUtils.VisitorListener() { boolean isNestedHandler(Element e) { return e.getParent() == boundElement && EventHandlerImpl.isEventHandler(e); } boolean isNestedLHHA(Element e) { return e.getParent() == boundElement && LHHA.isLHHA(e); } boolean mustFilterOut(Element e) { return excludeNestedHandlers && isNestedHandler(e) || excludeNestedLHHA && isNestedLHHA(e); } public void startElement(Element element) { // Handle xbl:content final boolean isXBLContent = element.getQName().equals(XBL_CONTENT_QNAME); final List<Node> resultingNodes; if (isXBLContent) { final String includesAttribute = element.attributeValue(XFormsConstants.INCLUDES_QNAME); final String scopeAttribute = element.attributeValue(XFormsConstants.XXBL_SCOPE_QNAME); final List<Node> contentToInsert; if (includesAttribute == null) { // All bound node content must be copied over (except nested handlers if requested) final List<Node> elementContent = Dom4jUtils.content(boundElement); final List<Node> clonedContent = new ArrayList<Node>(); for (final Node node : elementContent) { if (node instanceof Element) { final Element currentElement = (Element) node; if (!mustFilterOut(currentElement)) clonedContent.add(Dom4jUtils.copyElementCopyParentNamespaces(currentElement)); } else if (!(node instanceof Namespace)) { clonedContent.add(Dom4jUtils.createCopy(node)); } } contentToInsert = clonedContent; } else { // Apply CSS selector // Convert CSS to XPath final String xpathExpression = CSSParser.toXPath(includesAttribute); final NodeInfo boundElementInfo = documentWrapper.wrap(boundElement); // TODO: don't use getNamespaceContext() as this is already computed for the bound element final List<Object> elements = XPathCache.evaluate(boundElementInfo, xpathExpression, new NamespaceMapping(Dom4jUtils.getNamespaceContext(element)), null, null, null, null, null, null);// TODO: locationData if (elements.size() > 0) { // Clone all the resulting elements contentToInsert = new ArrayList<Node>(elements.size()); for (Object o : elements) { final NodeInfo currentNodeInfo = (NodeInfo) o; final Element currentElement = (Element) ((VirtualNode) currentNodeInfo) .getUnderlyingNode(); if (!mustFilterOut(currentElement)) contentToInsert.add(Dom4jUtils.copyElementCopyParentNamespaces(currentElement)); } } else { // Clone all the element's children if any // See: http://www.w3.org/TR/xbl/#the-content contentToInsert = new ArrayList<Node>(element.nodeCount()); for (Object o : element.elements()) { final Element currentElement = (Element) o; if (!mustFilterOut(currentElement)) contentToInsert.add(Dom4jUtils.copyElementCopyParentNamespaces(currentElement)); } } } // Insert content if any if (contentToInsert != null && contentToInsert.size() > 0) { final List<Node> parentContent = Dom4jUtils.content(element.getParent()); final int elementIndex = parentContent.indexOf(element); parentContent.addAll(elementIndex, contentToInsert); } // Remove <xbl:content> from shadow tree element.detach(); resultingNodes = contentToInsert; if (!StringUtils.isBlank(scopeAttribute)) { // If author specified scope attribute, use it setAttribute(resultingNodes, XFormsConstants.XXBL_SCOPE_QNAME, scopeAttribute, null); } else { // By default, set xxbl:scope="outer" on resulting elements setAttribute(resultingNodes, XFormsConstants.XXBL_SCOPE_QNAME, "outer", null); } } else { // Element is simply kept resultingNodes = Collections.singletonList((Node) element); } // Handle attribute forwarding final Attribute xblAttr = element.attribute(XBL_ATTR_QNAME); // standard xbl:attr (custom syntax) final Attribute xxblAttr = element.attribute(XXBL_ATTR_QNAME); // extension xxbl:attr (XPath expression) if (xblAttr != null) { // Detach attribute (not strictly necessary?) xblAttr.detach(); // Get attribute value final String xblAttrString = xblAttr.getValue(); final StringTokenizer st = new StringTokenizer(xblAttrString); while (st.hasMoreTokens()) { final String currentValue = st.nextToken(); final int equalIndex = currentValue.indexOf('='); if (equalIndex == -1) { // No a=b pair, just a single QName final QName valueQName = Dom4jUtils.extractTextValueQName(element, currentValue, true); if (!valueQName.getNamespaceURI().equals(XFormsConstants.XBL_NAMESPACE_URI)) { // This is not xbl:text, copy the attribute setAttribute(resultingNodes, valueQName, boundElement.attributeValue(valueQName), boundElement); } else { // This is xbl:text // "The xbl:text value cannot occur by itself in the list" } } else { // a=b pair final QName leftSideQName; { final String leftSide = currentValue.substring(0, equalIndex); leftSideQName = Dom4jUtils.extractTextValueQName(element, leftSide, true); } final QName rightSideQName; { final String rightSide = currentValue.substring(equalIndex + 1); rightSideQName = Dom4jUtils.extractTextValueQName(element, rightSide, true); } final boolean isLeftSideXBLText = leftSideQName.getNamespaceURI() .equals(XFormsConstants.XBL_NAMESPACE_URI); final boolean isRightSideXBLText = rightSideQName.getNamespaceURI() .equals(XFormsConstants.XBL_NAMESPACE_URI); final String rightSideValue; final Element namespaceElement; if (!isRightSideXBLText) { // Get attribute value rightSideValue = boundElement.attributeValue(rightSideQName); namespaceElement = boundElement; } else { // Get text value // "any text nodes (including CDATA nodes and whitespace text nodes) that are // explicit children of the bound element must have their data concatenated" rightSideValue = boundElement.getText();// must use getText() and not stringValue() namespaceElement = null; } if (rightSideValue != null) { // NOTE: XBL doesn't seem to says what should happen if the source attribute is not // found! We assume the rule is ignored in this case. if (!isLeftSideXBLText) { // Set attribute value setAttribute(resultingNodes, leftSideQName, rightSideValue, namespaceElement); } else { // Set text value // "value of the attribute on the right-hand side are to be represented as text // nodes underneath the shadow element" // TODO: "If the element has any child nodes in the DOM (any nodes, including // comment nodes, whitespace text nodes, or even empty CDATA nodes) then the pair // is in error and UAs must ignore it, meaning the attribute value is not forwarded" setText(resultingNodes, rightSideValue); } } } // TODO: handle xbl:lang? // TODO: handle type specifiers? } } else if (xxblAttr != null) { // Detach attribute (not strictly necessary?) xxblAttr.detach(); // Get attribute value final String xxblAttrString = xxblAttr.getValue(); final NodeInfo boundElementInfo = documentWrapper.wrap(boundElement); // TODO: don't use getNamespaceContext() as this is already computed for the bound element final List<Object> nodeInfos = XPathCache.evaluate(boundElementInfo, xxblAttrString, new NamespaceMapping(Dom4jUtils.getNamespaceContext(element)), null, null, null, null, null, null);// TODO: locationData if (nodeInfos.size() > 0) { for (Object nodeInfo : nodeInfos) { final NodeInfo currentNodeInfo = (NodeInfo) nodeInfo; if (currentNodeInfo.getNodeKind() == org.w3c.dom.Node.ATTRIBUTE_NODE) { // This is an attribute final Attribute currentAttribute = (Attribute) ((VirtualNode) currentNodeInfo) .getUnderlyingNode(); setAttribute(resultingNodes, currentAttribute.getQName(), currentAttribute.getValue(), currentAttribute.getParent()); } } } } // Prefix resulting xhtml:*/(@id |@for) // NOTE: We could also do the prefixing in the handlers, when the page is output. // // * Benefit of prefixing here: done statically // * Drawback of prefixing here: in the future if we try to reuse simple shadow trees this won't work // { // if (resultingNodes != null && resultingNodes.size() > 0) { // for (Iterator i = resultingNodes.iterator(); i.hasNext();) { // final Node node = (Node) i.next(); // if (node instanceof Element) { // Dom4jUtils.visitSubtree((Element) node, new Dom4jUtils.VisitorListener() { // public void startElement(Element element) { // if (XMLConstants.XHTML_NAMESPACE_URI.equals(element.getNamespaceURI())) { // // Found XHTML element // // // Update @id and @for if any // final Attribute idAttribute = element.attribute("id"); // if (idAttribute != null) { // idAttribute.setValue(prefix + idAttribute.getValue()); // } // final Attribute forAttribute = element.attribute("for"); // if (forAttribute != null) { // forAttribute.setValue(prefix + forAttribute.getValue()); // } // } // } // // public void endElement(Element element) { // } // // public void text(Text text) { // } // }); // } // } // } // } } private void setAttribute(List<Node> nodes, QName attributeQName, String attributeValue, Element namespaceElement) { if (nodes != null && nodes.size() > 0) { for (final Node node : nodes) { if (node instanceof Element) { final Element element = ((Element) node); // Copy missing namespaces, so that copying things like ref="foo:bar" work as expected Dom4jUtils.copyMissingNamespaces(namespaceElement, element); element.addAttribute(attributeQName, attributeValue); } } } } private void setText(List<Node> nodes, String value) { if (nodes != null && nodes.size() > 0) { for (final Node node : nodes) { if (node instanceof Element) { node.setText(value); } } } } public void endElement(Element element) { } public void text(Text text) { } }, true); return shadowTreeDocument; }
From source file:org.withinsea.izayoi.cortile.template.html.grammar.attr.core.ByContent.java
License:Mozilla Public License
@Override @SuppressWarnings("unchecked") @Priority(Priority.HIGHEST)/*w w w. ja v a2 s. c o m*/ public void processAttr(Attribute attr) throws CortileException { Element elem = attr.getParent(); String prefix = attr.getNamespacePrefix(); List<Attribute> attrs = new ArrayList<Attribute>((List<Attribute>) elem.attributes()); try { Element scope = elem; for (int i = attrs.indexOf(attr); i < attrs.size(); i++) { Attribute iAttr = attrs.get(i); String iAttrname = iAttr.getName(); if (iAttr.getNamespacePrefix().equals(prefix) && iAttrname.matches("^\\w+\\-content.*")) { Element contentScope = new DefaultElement(HTMLConstants.ANONYMOUS_TAG_NAME); DOMUtils.surroundInsideBy(scope, contentScope); iAttr.detach(); contentScope .add(new SurroundableAttr(new QName(iAttrname.replaceFirst("^(\\w+)\\-content", "$1"), iAttr.getQName().getNamespace()), iAttr.getValue())); for (int j = i + 1; j < attrs.size(); j++) { Attribute jAttr = attrs.get(j); jAttr.detach(); contentScope.add(jAttr); } scope = contentScope; } } } catch (Exception e) { throw new CortileException(e); } }