Java Code Examples for org.w3c.dom.Element#setPrefix()
The following examples show how to use
org.w3c.dom.Element#setPrefix() .
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: HeaderVerifier.java From cxf with Apache License 2.0 | 6 votes |
private void addPartialResponseHeader(SoapMessage message) { try { // add piggybacked wsa:From header to partial response List<Header> header = message.getHeaders(); Document doc = DOMUtils.getEmptyDocument(); SoapVersion ver = message.getVersion(); Element hdr = doc.createElementNS(ver.getHeader().getNamespaceURI(), ver.getHeader().getLocalPart()); hdr.setPrefix(ver.getHeader().getPrefix()); marshallFrom("urn:piggyback_responder", hdr, getMarshaller()); Element elem = DOMUtils.getFirstElement(hdr); while (elem != null) { Header holder = new Header( new QName(elem.getNamespaceURI(), elem.getLocalName()), elem, null); header.add(holder); elem = DOMUtils.getNextElement(elem); } } catch (Exception e) { verificationCache.put("SOAP header addition failed: " + e); e.printStackTrace(); } }
Example 2
Source File: DOMDifferenceEngineTest.java From xmlunit with Apache License 2.0 | 6 votes |
@Test public void compareElementsNS() { DOMDifferenceEngine d = new DOMDifferenceEngine(); DiffExpecter ex = new DiffExpecter(ComparisonType.ELEMENT_TAG_NAME); d.addDifferenceListener(ex); DifferenceEvaluator ev = new DifferenceEvaluator() { public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) { if (comparison.getType() == ComparisonType.NAMESPACE_PREFIX) { return ComparisonResult.EQUAL; } return outcome; } }; d.setComparisonController(ComparisonControllers.StopWhenDifferent); d.setDifferenceEvaluator(ev); Element e1 = doc.createElementNS("urn:xmlunit:test", "foo"); e1.setPrefix("p1"); Element e2 = doc.createElementNS("urn:xmlunit:test", "foo"); e2.setPrefix("p2"); assertEquals(wrap(ComparisonResult.EQUAL), d.compareNodes(e1, new XPathContext(), e2, new XPathContext())); assertEquals(0, ex.invoked); }
Example 3
Source File: XmlFilterReader.java From knox with Apache License 2.0 | 6 votes |
private Element bufferElement( StartElement event ) { QName qname = event.getName(); String prefix = qname.getPrefix(); String uri = qname.getNamespaceURI(); Element element; if( uri == null || uri.isEmpty() ) { element = document.createElement( qname.getLocalPart() ); } else { element = document.createElementNS( qname.getNamespaceURI(), qname.getLocalPart() ); if( prefix != null && !prefix.isEmpty() ) { element.setPrefix( prefix ); } } // Always need to buffer the namespaces regardless of what else happens so that XPath will work on attributes // namespace qualified attributes. bufferNamespaces( event, element ); return element; }
Example 4
Source File: XmlDocument.java From development with Apache License 2.0 | 6 votes |
/** * Converts the given technical products to a dom document. * * @param techProds * the technical products to convert * @param localizer * the localizer used to read localized values * @param dm * the data service used to read other values * @return the dom document */ public XmlDocument(String namespace, String namespacePrefix, String schemaLocation, String rootNodeName) { set(namespace, namespacePrefix, schemaLocation, rootNodeName); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new SaaSSystemException(e); } xmldoc = builder.getDOMImplementation().createDocument(getNameSpace(), getRootNodeName(), null); root = (Element) xmldoc.getFirstChild(); root.setPrefix(getPrefix()); root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.setAttribute("xsi:schemaLocation", getSchemaLocation()); }
Example 5
Source File: NodesTest.java From caja with Apache License 2.0 | 6 votes |
public final void testRenderWithUnknownNamespace() throws Exception { DocumentFragment fragment = xmlFragment(fromString( "" + "<foo xmlns='http://www.w3.org/XML/1998/namespace'" + " xmlns:bar='http://bobs.house.of/XML&BBQ'>" + "<bar:baz boo='howdy' xml:lang='es'/>" + "</foo>")); // Remove any XMLNS attributes and prefixes. Element el = (Element) fragment.getFirstChild(); while (el.getAttributes().getLength() != 0) { el.removeAttributeNode((Attr) el.getAttributes().item(0)); } el.setPrefix(""); el.getFirstChild().setPrefix(""); assertEquals( "" + "<xml:foo>" + "<_ns1:baz xmlns:_ns1=\"http://bobs.house.of/XML&BBQ\"" + " boo=\"howdy\" xml:lang=\"es\"></_ns1:baz>" + "</xml:foo>", Nodes.render(fragment, MarkupRenderMode.XML)); }
Example 6
Source File: TolerantSaxDocumentBuilder.java From xmlunit with Apache License 2.0 | 6 votes |
/** * Create a DOM Element for insertion into the current document * @param namespaceURI * @param qName * @param attributes * @return the created Element */ private Element createElement(String namespaceURI, String qName, Attributes attributes) { Element newElement = currentDocument.createElement(qName); if (namespaceURI != null && namespaceURI.length() > 0) { newElement.setPrefix(namespaceURI); } for(int i = 0; attributes != null && i < attributes.getLength(); ++i) { newElement.setAttribute(attributes.getQName(i), attributes.getValue(i)); } return newElement; }
Example 7
Source File: ServiceModelPolicyUpdater.java From cxf with Apache License 2.0 | 5 votes |
private void addPolicyRef(Extensible ext, Policy p) { Document doc = DOMUtils.getEmptyDocument(); Element el = doc.createElementNS(p.getNamespace(), Constants.ELEM_POLICY_REF); el.setPrefix(Constants.ATTR_WSP); el.setAttribute(Constants.ATTR_URI, "#" + p.getId()); UnknownExtensibilityElement uee = new UnknownExtensibilityElement(); uee.setElementType(new QName(p.getNamespace(), Constants.ELEM_POLICY_REF)); uee.setElement(el); uee.setRequired(true); ext.addExtensor(uee); }
Example 8
Source File: AbstractAegisTest.java From cxf with Apache License 2.0 | 5 votes |
protected Element createElement(String namespace, String name, String namespacePrefix) { //The Aegis tests use xpaths that require the created element be the root of a doc Document doc = DOMUtils.createDocument(); Element element = doc.createElementNS(namespace, name); if (namespacePrefix != null) { element.setPrefix(namespacePrefix); DOMUtils.addNamespacePrefix(element, namespace, namespacePrefix); } doc.appendChild(element); return element; }
Example 9
Source File: InstanceService.java From container with Apache License 2.0 | 5 votes |
private Document createServiceInstanceInitialPropertiesFromServiceTemplate(final CsarId csarId, final String serviceTemplateId) { final Document existingProperties = this.serviceTemplateService.getPropertiesOfServiceTemplate(csarId, serviceTemplateId); if (existingProperties != null) { return existingProperties; } logger.debug("No Properties found in BoundaryDefinitions for ST {} thus creating blank ones", serviceTemplateId); final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); final Document doc = db.newDocument(); final Element createElementNS = doc.createElementNS("http://docs.oasis-open.org/tosca/ns/2011/12", "Properties"); createElementNS.setAttribute("xmlns:tosca", "http://docs.oasis-open.org/tosca/ns/2011/12"); createElementNS.setPrefix("tosca"); doc.appendChild(createElementNS); return doc; } catch (final ParserConfigurationException e) { logger.info("Cannot create a new DocumentBuilder: {}", e.getMessage()); } return null; // this should never happen }
Example 10
Source File: DomHelper.java From jaxb2-maven-plugin with Apache License 2.0 | 5 votes |
/** * <p>Adds the given formattedDocumentation within an XML documentation annotation under the supplied Node. * Only adds the documentation annotation if the formattedDocumentation is non-null and non-empty. The * documentation annotation is on the form:</p> * <pre> * <code> * <xs:annotation> * <xs:documentation>(JavaDoc here, within a CDATA section)</xs:documentation> * </xs:annotation> * </code> * </pre> * * @param aNode The non-null Node to which an XML documentation annotation should be added. * @param formattedDocumentation The documentation text to add. */ public static void addXmlDocumentAnnotationTo(final Node aNode, final String formattedDocumentation) { if (aNode != null && formattedDocumentation != null && !formattedDocumentation.isEmpty()) { // Add the new Elements, as required. final Document doc = aNode.getOwnerDocument(); final Element annotation = doc.createElementNS( XMLConstants.W3C_XML_SCHEMA_NS_URI, ANNOTATION_ELEMENT_NAME); final Element docElement = doc.createElementNS( XMLConstants.W3C_XML_SCHEMA_NS_URI, DOCUMENTATION_ELEMENT_NAME); final CDATASection xsdDocumentation = doc.createCDATASection(formattedDocumentation); // Set the prefixes annotation.setPrefix(XSD_SCHEMA_NAMESPACE_PREFIX); docElement.setPrefix(XSD_SCHEMA_NAMESPACE_PREFIX); // Inject the formattedDocumentation into the CDATA section. annotation.appendChild(docElement); final Node firstChildOfCurrentNode = aNode.getFirstChild(); if (firstChildOfCurrentNode == null) { aNode.appendChild(annotation); } else { aNode.insertBefore(annotation, firstChildOfCurrentNode); } // All Done. docElement.appendChild(xsdDocumentation); } }
Example 11
Source File: XPath2FilterTransform.java From dss with GNU Lesser General Public License v2.1 | 5 votes |
@Override public Element createTransform(Document document, Element parentNode) { final Element transform = DomUtils.addElement(document, parentNode, namespace, XMLDSigElement.TRANSFORM); transform.setAttribute(XMLDSigAttribute.ALGORITHM.getAttributeName(), algorithm); // XPath element must have a specific namespace Element xPathElement = DomUtils.addTextElement(document, transform, XAdESNamespaces.XMLDSIG_FILTER2, XMLDSigElement.XPATH, xPathExpression); xPathElement.setPrefix(XAdESNamespaces.XMLDSIG_FILTER2.getPrefix()); xPathElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + XAdESNamespaces.XMLDSIG_FILTER2.getPrefix(), XAdESNamespaces.XMLDSIG_FILTER2.getUri()); xPathElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + namespace.getPrefix(), namespace.getUri()); xPathElement.setAttribute(FILTER_ATTRIBUTE, filter); return xPathElement; }
Example 12
Source File: ElementImpl.java From marklogic-contentpump with Apache License 2.0 | 5 votes |
protected Node cloneNode(Document doc, boolean deep) { Element elem = doc.createElementNS(getNamespaceURI(), getTagName()); elem.setPrefix(getPrefix()); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (attr instanceof AttrImpl) { elem.setAttributeNode((Attr) ((AttrImpl) attr).cloneNode(doc, deep)); } else { // ns decl, stored as Java DOM Attr // uri of xmlns is "http://www.w3.org/2000/xmlns/" Attr clonedAttr = doc.createAttributeNS( "http://www.w3.org/2000/xmlns/", attr.getName()); clonedAttr.setValue(attr.getValue()); elem.setAttributeNode(clonedAttr); } } if (deep) { // clone children NodeList list = getChildNodes(); for (int i = 0; i < list.getLength(); i++) { NodeImpl n = (NodeImpl) list.item(i); Node c = n.cloneNode(doc, true); elem.appendChild(c); } } return elem; }
Example 13
Source File: AbstractXMLObjectMarshaller.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Marshalls the namespace prefix of the XMLObject into the DOM element. * * @param xmlObject the XMLObject being marshalled * @param domElement the DOM element the XMLObject is being marshalled into */ protected void marshallNamespacePrefix(XMLObject xmlObject, Element domElement) { String prefix = xmlObject.getElementQName().getPrefix(); prefix = DatatypeHelper.safeTrimOrNullString(prefix); if (prefix != null) { domElement.setPrefix(prefix); } }
Example 14
Source File: NodesTest.java From xmlunit with Apache License 2.0 | 5 votes |
@Test public void qNameOfElementWithNsAndPrefix() { Element e = doc.createElementNS(SOME_URI, FOO); e.setPrefix(BAR); QName q = Nodes.getQName(e); assertEquals(FOO, q.getLocalPart()); assertEquals(SOME_URI, q.getNamespaceURI()); assertEquals(BAR, q.getPrefix()); assertEquals(new QName(SOME_URI, FOO), q); assertEquals(new QName(SOME_URI, FOO, BAR), q); }
Example 15
Source File: AbstractDocumentComponent.java From netbeans with Apache License 2.0 | 5 votes |
private void ensurePrefixDeclaredFor(Element newComponentElement, String newComponentNS) { String existingPrefix = lookupPrefix(newComponentNS); String prefix = newComponentElement.getPrefix(); if (existingPrefix == null) { if (prefix == null) { prefix = "ns"; //NOI18N } prefix = ensureUnique(prefix, newComponentNS); ((AbstractDocumentComponent)getModel().getRootComponent()). addPrefix(prefix, newComponentNS); newComponentElement.setPrefix(prefix); } else { newComponentElement.setPrefix(existingPrefix); } }
Example 16
Source File: SdkRepoSource.java From java-n-IDE-for-Android with Apache License 2.0 | 4 votes |
/** * Helper method used by {@link #findAlternateToolsXml(InputStream)} to duplicate a node * and attach it to the given root in the new document. */ private Element duplicateNode(Element newRootNode, Element oldNode, String namespaceUri, String prefix) { // The implementation here is more or less equivalent to // // newRoot.appendChild(newDoc.importNode(oldNode, deep=true)) // // except we can't just use importNode() since we need to deal with the fact // that the old document is not namespace-aware yet the new one is. Document newDoc = newRootNode.getOwnerDocument(); Element newNode = null; String nodeName = oldNode.getNodeName(); int pos = nodeName.indexOf(':'); if (pos > 0 && pos < nodeName.length() - 1) { nodeName = nodeName.substring(pos + 1); newNode = newDoc.createElementNS(namespaceUri, nodeName); newNode.setPrefix(prefix); } else { newNode = newDoc.createElement(nodeName); } newRootNode.appendChild(newNode); // Merge in all the attributes NamedNodeMap attrs = oldNode.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); Attr newAttr = null; String attrName = attr.getNodeName(); pos = attrName.indexOf(':'); if (pos > 0 && pos < attrName.length() - 1) { attrName = attrName.substring(pos + 1); newAttr = newDoc.createAttributeNS(namespaceUri, attrName); newAttr.setPrefix(prefix); } else { newAttr = newDoc.createAttribute(attrName); } newAttr.setNodeValue(attr.getNodeValue()); if (pos > 0) { newNode.getAttributes().setNamedItemNS(newAttr); } else { newNode.getAttributes().setNamedItem(newAttr); } } // Merge all child elements and texts for (Node child = oldNode.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { duplicateNode(newNode, (Element) child, namespaceUri, prefix); } else if (child.getNodeType() == Node.TEXT_NODE) { Text newText = newDoc.createTextNode(child.getNodeValue()); newNode.appendChild(newText); } } return newNode; }
Example 17
Source File: LocationAwareContentHandler.java From windup with Eclipse Public License 1.0 | 4 votes |
@Override public void startElement(final String uri, final String localName, final String qName, final Attributes attrs) throws SAXException { Element e = null; if (localName != null && !"".equals(localName)) { e = doc.createElementNS(uri, localName); if (qName.contains(":")) e.setPrefix(qName.substring(0, qName.indexOf(":"))); } else { e = doc.createElement(qName); } storeLineInformation(e); if(StringUtils.isNotBlank(uri)) { namespaceURIs.add(uri); } if(doc.getUserData(NAMESPACE_KEY_NAME) == null) { doc.setUserData(NAMESPACE_KEY_NAME, namespaceURIs, null); } if (current == null) { doc.appendChild(e); } else { current.appendChild(e); } current = e; if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Attr attr = null; if (attrs.getLocalName(i) != null && !"".equals(attrs.getLocalName(i))) { attr = doc.createAttributeNS(attrs.getURI(i), attrs.getLocalName(i)); attr.setValue(attrs.getValue(i)); storeLineInformation(attr); current.setAttributeNodeNS(attr); } else { attr = doc.createAttribute(attrs.getQName(i)); attr.setValue(attrs.getValue(i)); storeLineInformation(attr); current.setAttributeNode(attr); } } } }
Example 18
Source File: SdkRepoSource.java From javaide with GNU General Public License v3.0 | 4 votes |
/** * Helper method used by {@link #findAlternateToolsXml(InputStream)} to duplicate a node * and attach it to the given root in the new document. */ private Element duplicateNode(Element newRootNode, Element oldNode, String namespaceUri, String prefix) { // The implementation here is more or less equivalent to // // newRoot.appendChild(newDoc.importNode(oldNode, deep=true)) // // except we can't just use importNode() since we need to deal with the fact // that the old document is not namespace-aware yet the new one is. Document newDoc = newRootNode.getOwnerDocument(); Element newNode = null; String nodeName = oldNode.getNodeName(); int pos = nodeName.indexOf(':'); if (pos > 0 && pos < nodeName.length() - 1) { nodeName = nodeName.substring(pos + 1); newNode = newDoc.createElementNS(namespaceUri, nodeName); newNode.setPrefix(prefix); } else { newNode = newDoc.createElement(nodeName); } newRootNode.appendChild(newNode); // Merge in all the attributes NamedNodeMap attrs = oldNode.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Attr attr = (Attr) attrs.item(i); Attr newAttr = null; String attrName = attr.getNodeName(); pos = attrName.indexOf(':'); if (pos > 0 && pos < attrName.length() - 1) { attrName = attrName.substring(pos + 1); newAttr = newDoc.createAttributeNS(namespaceUri, attrName); newAttr.setPrefix(prefix); } else { newAttr = newDoc.createAttribute(attrName); } newAttr.setNodeValue(attr.getNodeValue()); if (pos > 0) { newNode.getAttributes().setNamedItemNS(newAttr); } else { newNode.getAttributes().setNamedItem(newAttr); } } // Merge all child elements and texts for (Node child = oldNode.getFirstChild(); child != null; child = child.getNextSibling()) { if (child.getNodeType() == Node.ELEMENT_NODE) { duplicateNode(newNode, (Element) child, namespaceUri, prefix); } else if (child.getNodeType() == Node.TEXT_NODE) { Text newText = newDoc.createTextNode(child.getNodeValue()); newNode.appendChild(newText); } } return newNode; }
Example 19
Source File: SerializableMetadata.java From oodt with Apache License 2.0 | 4 votes |
public Document toXML() throws IOException { try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); factory.setNamespaceAware(true); Document document = factory.newDocumentBuilder().newDocument(); Element root = document.createElementNS("http://oodt.jpl.nasa.gov/1.0/cas", "metadata"); root.setPrefix("cas"); document.appendChild(root); // now add the set of metadata elements in the properties object for (String key : this.getAllKeys()) { Element metadataElem = document.createElement("keyval"); Element keyElem = document.createElement("key"); if (this.useCDATA) { keyElem.appendChild(document.createCDATASection(key)); } else { keyElem.appendChild(document.createTextNode(URLEncoder.encode(key, this.xmlEncoding))); } metadataElem.appendChild(keyElem); metadataElem.setAttribute("type", "vector"); for (String value : this.getAllMetadata(key)) { Element valElem = document.createElement("val"); if (value == null) { throw new Exception("Attempt to write null value " + "for property: [" + key + "]: val: [null]"); } if (this.useCDATA) { valElem.appendChild(document .createCDATASection(value)); } else { valElem.appendChild(document.createTextNode(URLEncoder .encode(value, this.xmlEncoding))); } metadataElem.appendChild(valElem); } root.appendChild(metadataElem); } return document; } catch (Exception e) { LOG.log(Level.SEVERE, e.getMessage()); throw new IOException( "Failed to create XML DOM Document for SerializableMetadata : " + e.getMessage()); } }
Example 20
Source File: ManifestGenerator.java From sakai with Educational Community License v2.0 | 3 votes |
private Element createLOMElement(String elename) { Element imsmdlom = getDocument().createElementNS(IMSMD_NAMESPACE_URI, elename); imsmdlom.setPrefix("imsmd"); return imsmdlom; }