Java Code Examples for org.w3c.dom.Element#getPrefix()
The following examples show how to use
org.w3c.dom.Element#getPrefix() .
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: SardineUtil.java From sardine-android with Apache License 2.0 | 6 votes |
public static QName toQName(Element element) { String namespace = element.getNamespaceURI(); if (namespace == null) { return new QName(SardineUtil.DEFAULT_NAMESPACE_URI, element.getLocalName(), SardineUtil.DEFAULT_NAMESPACE_PREFIX); } else if (element.getPrefix() == null) { return new QName(element.getNamespaceURI(), element.getLocalName()); } else { return new QName(element.getNamespaceURI(), element.getLocalName(), element.getPrefix()); } }
Example 2
Source File: SAAJMessage.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private void addAttributes(Element e, NamedNodeMap attrs) { if(attrs == null) return; String elPrefix = e.getPrefix(); for(int i=0; i < attrs.getLength();i++) { Attr a = (Attr)attrs.item(i); //check if attr is ns declaration if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) { if(elPrefix == null && a.getLocalName().equals("xmlns")) { // the target element has already default ns declaration, dont' override it continue; } else if(elPrefix != null && "xmlns".equals(a.getPrefix()) && elPrefix.equals(a.getLocalName())) { //dont bind the prefix to ns again, its already in the target element. continue; } e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue()); continue; } e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue()); } }
Example 3
Source File: BodyImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
QName getPayloadQName() { if (staxBridge != null) { return staxBridge.getPayloadQName(); } else { //not lazy - Just get first child element and return its name Element elem = getFirstChildElement(); if (elem != null) { String ns = elem.getNamespaceURI(); String pref = elem.getPrefix(); String local = elem.getLocalName(); if (pref != null) return new QName(ns, local, pref); if (ns != null) return new QName(ns, local); return new QName(local); } } return null; }
Example 4
Source File: XSLTProcessor.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
/** * Adds the specified parameters to the xsl template as variables within the alfresco namespace. * * @param xsltModel * the variables to place within the xsl template * @param xslTemplate * the xsl template */ protected void addParameters(final XSLTemplateModel xsltModel, final Document xslTemplate) { final Element docEl = xslTemplate.getDocumentElement(); final String XSL_NS = docEl.getNamespaceURI(); final String XSL_NS_PREFIX = docEl.getPrefix(); for (Map.Entry<QName, Object> e : xsltModel.entrySet()) { if (ROOT_NAMESPACE.equals(e.getKey())) { continue; } final Element el = xslTemplate.createElementNS(XSL_NS, XSL_NS_PREFIX + ":variable"); el.setAttribute("name", e.getKey().toPrefixString()); final Object o = e.getValue(); if (o instanceof String || o instanceof Number || o instanceof Boolean) { el.appendChild(xslTemplate.createTextNode(o.toString())); // ALF-15413. Add the variables at the end of the list of children docEl.insertBefore(el, null); } } }
Example 5
Source File: SAAJMessage.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private void addAttributes(Element e, NamedNodeMap attrs) { if(attrs == null) return; String elPrefix = e.getPrefix(); for(int i=0; i < attrs.getLength();i++) { Attr a = (Attr)attrs.item(i); //check if attr is ns declaration if("xmlns".equals(a.getPrefix()) || "xmlns".equals(a.getLocalName())) { if(elPrefix == null && a.getLocalName().equals("xmlns")) { // the target element has already default ns declaration, dont' override it continue; } else if(elPrefix != null && "xmlns".equals(a.getPrefix()) && elPrefix.equals(a.getLocalName())) { //dont bind the prefix to ns again, its already in the target element. continue; } e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue()); continue; } e.setAttributeNS(a.getNamespaceURI(),a.getName(),a.getValue()); } }
Example 6
Source File: POMComponentFactoryImpl.java From netbeans with Apache License 2.0 | 6 votes |
private static QName getQName(Element element, POMComponentImpl context) { String namespace = element.getNamespaceURI(); String prefix = element.getPrefix(); if (namespace == null && context != null) { namespace = context.lookupNamespaceURI(prefix); } String localName = element.getLocalName(); assert(localName != null); if (namespace == null && prefix == null) { return new QName(localName); } else if (namespace != null && prefix == null) { return new QName(namespace, localName); } else { return new QName(namespace, localName, prefix); } }
Example 7
Source File: DOMWriter.java From java-client-api with Apache License 2.0 | 5 votes |
public void serializeElement(Element element) throws XMLStreamException { String namespaceURI = element.getNamespaceURI(); String prefix = (namespaceURI != null) ? element.getPrefix() : null; String localName = (namespaceURI != null) ? element.getLocalName() : element.getTagName(); if (element.hasChildNodes()) { if (prefix != null) { serializer.writeStartElement(prefix, localName, namespaceURI); } else if (namespaceURI != null) { serializer.writeStartElement("", localName, namespaceURI); } else { serializer.writeStartElement(localName); } if (element.hasAttributes()) { serializeAttributes(element.getAttributes()); } serializeNodeList(element.getChildNodes()); serializer.writeEndElement(); } else { if (prefix != null) { serializer.writeEmptyElement(prefix, localName, namespaceURI); } else if (namespaceURI != null) { serializer.writeEmptyElement("", localName, namespaceURI); } else { serializer.writeEmptyElement(localName); } if (element.hasAttributes()) { serializeAttributes(element.getAttributes()); } } }
Example 8
Source File: SignatureUnmarshaller.java From lams with GNU General Public License v2.0 | 5 votes |
/** {@inheritDoc} */ public Signature unmarshall(Element signatureElement) throws UnmarshallingException { log.debug("Starting to unmarshall Apache XML-Security-based SignatureImpl element"); SignatureImpl signature = new SignatureImpl(signatureElement.getNamespaceURI(), signatureElement.getLocalName(), signatureElement.getPrefix()); try { log.debug("Constructing Apache XMLSignature object"); XMLSignature xmlSignature = new XMLSignature(signatureElement, ""); SignedInfo signedInfo = xmlSignature.getSignedInfo(); log.debug("Adding canonicalization and signing algorithms, and HMAC output length to Signature"); signature.setCanonicalizationAlgorithm(signedInfo.getCanonicalizationMethodURI()); signature.setSignatureAlgorithm(signedInfo.getSignatureMethodURI()); signature.setHMACOutputLength(getHMACOutputLengthValue(signedInfo.getSignatureMethodElement())); org.apache.xml.security.keys.KeyInfo xmlSecKeyInfo = xmlSignature.getKeyInfo(); if (xmlSecKeyInfo != null) { log.debug("Adding KeyInfo to Signature"); Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller( xmlSecKeyInfo.getElement()); KeyInfo keyInfo = (KeyInfo) unmarshaller.unmarshall(xmlSecKeyInfo.getElement()); signature.setKeyInfo(keyInfo); } signature.setXMLSignature(xmlSignature); signature.setDOM(signatureElement); return signature; } catch (XMLSecurityException e) { log.error("Error constructing Apache XMLSignature instance from Signature element: {}", e.getMessage()); throw new UnmarshallingException("Unable to unmarshall Signature with Apache XMLSignature", e); } }
Example 9
Source File: TestSerializableMetadata.java From oodt with Apache License 2.0 | 5 votes |
public void testNamespace() throws Exception { SerializableMetadata metadata = new SerializableMetadata(); metadata.addMetadata("Name1", "Value1"); metadata.addMetadata("Name2", "Value2"); // write xml DOM to string ByteArrayOutputStream array = new ByteArrayOutputStream(); metadata.writeMetadataToXmlStream(array); // read string into new xml DOM DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); f.setNamespaceAware(true); Document doc = f.newDocumentBuilder().parse(new ByteArrayInputStream(array.toByteArray())); final String NS = "http://oodt.jpl.nasa.gov/1.0/cas"; final String PREFIX = "cas"; // compare namespaces in DOM before and after the write/read operation Element before = metadata.toXML().getDocumentElement(); String nsBefore = before.getNamespaceURI(); String preBefore = before.getPrefix(); assertEquals(NS, nsBefore); assertEquals(PREFIX, preBefore); Element after = doc.getDocumentElement(); String nsAfter = after.getNamespaceURI(); String preAfter = after.getPrefix(); assertEquals(NS, nsAfter); assertEquals(PREFIX, preAfter); }
Example 10
Source File: CMXSDElementDeclaration.java From lemminx with Eclipse Public License 2.0 | 5 votes |
private static QName createQName(Element tag) { // intern must be called since Xerces uses == to compare String ? // -> see // https://github.com/apache/xerces2-j/blob/trunk/src/org/apache/xerces/impl/xs/SubstitutionGroupHandler.java#L55 String namespace = tag.getNamespaceURI(); return new QName(tag.getPrefix(), tag.getLocalName().intern(), tag.getTagName().intern(), StringUtils.isEmpty(namespace) ? null : namespace.intern()); }
Example 11
Source File: ObjectTreeValidationUsingSplitter.java From citygml4j with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws Exception { /* * PLEASE NOTE, that you receive less errors if the in-memory objects * derived from the input document are validated than if the input document * itself is validated. * reason: citygml4j tries to reconstruct a valid object tree from the * input document. Generally, this means * 1) Invalid order of XML elements will be corrected automatically (see ADDRESS element) * 2) Invalid text values of XML elements cannot be automatically corrected * and thus will be reported (see, e.g., gml:id of BUILDING element) * 3) Invalid XML child elements will be omitted in the object tree (see CLOSURESURFACE element) * * Due to 3) you should always make sure to generate object trees from * valid CityGML documents! */ SimpleDateFormat df = new SimpleDateFormat("[HH:mm:ss] "); System.out.println(df.format(new Date()) + "setting up citygml4j context and CityGML builder"); CityGMLContext ctx = CityGMLContext.getInstance(); CityGMLBuilder builder = ctx.createCityGMLBuilder(); System.out.println(df.format(new Date()) + "parsing ADE schema file CityGML-SubsurfaceADE-0_9_0.xsd"); SchemaHandler schemaHandler = SchemaHandler.newInstance(); schemaHandler.parseSchema(new File("datasets/schemas/CityGML-SubsurfaceADE-0_9_0.xsd")); System.out.println(df.format(new Date()) + "reading ADE-enriched CityGML file LOD2_SubsurfaceStructureADE_invalid_v100.gml"); CityGMLInputFactory in = builder.createCityGMLInputFactory(schemaHandler); in.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.NO_SPLIT); CityGMLReader reader = in.createCityGMLReader(new File("datasets/LOD2_SubsurfaceStructureADE_invalid_v100.gml")); CityGML citygml = reader.nextFeature(); reader.close(); System.out.println(df.format(new Date()) + "creating citygml4j Validator"); Validator validator = builder.createValidator(schemaHandler); validator.setValidationEventHandler(event -> { System.out.println("\t" + event.getMessage()); return true; }); System.out.println(df.format(new Date()) + "creating citygml4j FeatureSplitter and splitting document into single features"); FeatureSplitter splitter = new FeatureSplitter() .setSchemaHandler(schemaHandler) .setSplitMode(FeatureSplitMode.SPLIT_PER_FEATURE) .splitCopy(true); System.out.println(df.format(new Date()) + "iterating over splitting result and validating features against CityGML 1.0.0"); for (CityGML feature : splitter.split(citygml)) { String type; if (feature instanceof ADEGenericElement){ Element element = ((ADEGenericElement)feature).getContent(); type = element.getPrefix() + ':' + element.getLocalName(); } else type = feature.getCityGMLClass().toString(); System.out.println("Validating " + type); validator.validate(feature, CityGMLVersion.v1_0_0); } System.out.println(df.format(new Date()) + "sample citygml4j application successfully finished"); }
Example 12
Source File: ElementFactory.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Create element wrapper for existing DOM element. * * @param ownerDocument SOAP document wrapper not null * @param element DOM element not null * @return SOAP wrapper for DOM element */ public static SOAPElement createElement(SOAPDocumentImpl ownerDocument, Element element) { Objects.requireNonNull(ownerDocument); Objects.requireNonNull(element); String localName = element.getLocalName(); String namespaceUri = element.getNamespaceURI(); String prefix = element.getPrefix(); if ("Envelope".equalsIgnoreCase(localName)) { if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new Envelope1_1Impl(ownerDocument, element); } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) { return new Envelope1_2Impl(ownerDocument, element); } } if ("Body".equalsIgnoreCase(localName)) { if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new Body1_1Impl(ownerDocument, element); } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) { return new Body1_2Impl(ownerDocument, element); } } if ("Header".equalsIgnoreCase(localName)) { if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new Header1_1Impl(ownerDocument, element); } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) { return new Header1_2Impl(ownerDocument, element); } } if ("Fault".equalsIgnoreCase(localName)) { if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new Fault1_1Impl(ownerDocument, element); } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) { return new Fault1_2Impl(ownerDocument, element); } } if ("Detail".equalsIgnoreCase(localName)) { if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new Detail1_1Impl(ownerDocument, element); } else if (NameImpl.SOAP12_NAMESPACE.equals(namespaceUri)) { return new Detail1_2Impl(ownerDocument, element); } } if ("faultcode".equalsIgnoreCase(localName) || "faultstring".equalsIgnoreCase(localName) || "faultactor".equalsIgnoreCase(localName)) { // SOAP 1.2 does not have fault(code/string/actor) // So there is no else case required if (NameImpl.SOAP11_NAMESPACE.equals(namespaceUri)) { return new FaultElement1_1Impl(ownerDocument, localName, prefix); } } return new ElementImpl(ownerDocument, element); }
Example 13
Source File: SchemaInfo.java From cxf with Apache License 2.0 | 4 votes |
/** * Build and return a DOM tree for this schema. * @return a DOM Element representation of the schema */ public synchronized Element getElement() { // if someone recently used this DOM tree, take advantage. Element element = cachedElement == null ? null : cachedElement.get(); if (element != null) { return element; } if (getSchema() == null) { throw new RuntimeException("No XmlSchema in SchemaInfo"); } XmlSchema sch = getSchema(); synchronized (sch) { XmlSchema schAgain = getSchema(); // XML Schema blows up when the context is null as opposed to empty. // Some unit tests really want to see 'tns:'. if (schAgain.getNamespaceContext() == null) { NamespaceMap nsMap = new NamespaceMap(); nsMap.add("xsd", Constants.URI_2001_SCHEMA_XSD); nsMap.add("tns", schAgain.getTargetNamespace()); schAgain.setNamespaceContext(nsMap); } Document serializedSchema; try { serializedSchema = schAgain.getSchemaDocument(); } catch (XmlSchemaSerializerException e) { throw new RuntimeException("Error serializing Xml Schema", e); } element = serializedSchema.getDocumentElement(); cachedElement = new SoftReference<>(element); } // A problem can occur with the ibm jdk when the XmlSchema // object is serialized. The xmlns declaration gets incorrectly // set to the same value as the targetNamespace attribute. // The aegis databinding tests demonstrate this particularly. if (element.getPrefix() == null && !Constants.URI_2001_SCHEMA_XSD.equals(element.getAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE))) { Attr attr = element.getOwnerDocument() .createAttributeNS(Constants.XMLNS_ATTRIBUTE_NS_URI, Constants.XMLNS_ATTRIBUTE); attr.setValue(Constants.URI_2001_SCHEMA_XSD); element.setAttributeNodeNS(attr); } return element; }
Example 14
Source File: XMLUtil.java From TVRemoteIME with GNU General Public License v2.0 | 4 votes |
protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI) throws Exception { StringBuilder b = new StringBuilder(); if (node == null) { return ""; } if (node instanceof Element) { Element element = (Element) node; b.append("<"); b.append(element.getNodeName()); Map<String, String> thisLevelPrefixes = new HashMap(); if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) { thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI()); } if (element.hasAttributes()) { NamedNodeMap map = element.getAttributes(); for (int i = 0; i < map.getLength(); i++) { Node attr = map.item(i); if (attr.getNodeName().startsWith("xmlns")) continue; if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) { thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI()); } b.append(" "); b.append(attr.getNodeName()); b.append("=\""); b.append(attr.getNodeValue()); b.append("\""); } } if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI) && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) { b.append(" xmlns=\"").append(namespaceURI).append("\""); } for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) { b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\""); parentPrefixes.add(entry.getKey()); } NodeList children = element.getChildNodes(); boolean hasOnlyAttributes = true; for (int i = 0; i < children.getLength(); i++) { Node child = children.item(i); if (child.getNodeType() != Node.ATTRIBUTE_NODE) { hasOnlyAttributes = false; break; } } if (!hasOnlyAttributes) { b.append(">"); for (int i = 0; i < children.getLength(); i++) { b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI())); } b.append("</"); b.append(element.getNodeName()); b.append(">"); } else { b.append("/>"); } for (String thisLevelPrefix : thisLevelPrefixes.keySet()) { parentPrefixes.remove(thisLevelPrefix); } } else if (node.getNodeValue() != null) { b.append(encodeText(node.getNodeValue())); } return b.toString(); }
Example 15
Source File: NameImpl.java From openjdk-8 with GNU General Public License v2.0 | 4 votes |
public static Name copyElementName(Element element) { String localName = element.getLocalName(); String prefix = element.getPrefix(); String uri = element.getNamespaceURI(); return create(localName, prefix, uri); }
Example 16
Source File: NameImpl.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
public static Name copyElementName(Element element) { String localName = element.getLocalName(); String prefix = element.getPrefix(); String uri = element.getNamespaceURI(); return create(localName, prefix, uri); }
Example 17
Source File: RuleDefinition.java From java-client-api with Apache License 2.0 | 4 votes |
private void receiveRuleMetadataImpl(Element ruleElement) { RuleMetadata ruleMetadata = getMetadata(); ruleMetadata.clear(); Node metadataContainer = ruleElement.getElementsByTagNameNS( RequestConstants.RESTAPI_NS, "rule-metadata").item(0); if (metadataContainer == null) return; NodeList metadataIn = metadataContainer.getChildNodes(); for (int i = 0; i < metadataIn.getLength(); i++) { Node node = metadataIn.item(i); if (node.getNodeType() != Node.ELEMENT_NODE) continue; Element metadataElement = (Element) node; QName metadataPropertyName = null; String namespaceURI = metadataElement.getNamespaceURI(); if (namespaceURI != null) { String prefix = metadataElement.getPrefix(); if (prefix != null) { metadataPropertyName = new QName(namespaceURI, metadataElement.getLocalName(), prefix); } else { metadataPropertyName = new QName(namespaceURI, metadataElement.getTagName()); } } else { metadataPropertyName = new QName(metadataElement.getTagName()); } if (!metadataElement.hasChildNodes()) { metadata.put(metadataPropertyName, (String) null); continue; } NodeList children = metadataElement.getChildNodes(); boolean hasChildElements = false; int childCount = children.getLength(); for (int j = 0; j < childCount; j++) { Node child = children.item(j); if (child.getNodeType() == Node.ELEMENT_NODE) { hasChildElements = true; break; } } if (hasChildElements) { metadata.put(metadataPropertyName, children); continue; } String value = metadataElement.getTextContent(); if (metadataElement.hasAttributeNS( XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type")) { String type = metadataElement.getAttributeNS( XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); metadata.put(metadataPropertyName, ValueConverter.convertToJava(type, value)); continue; } else { metadata.put(metadataPropertyName, value); } metadata.put(metadataPropertyName, value); } }
Example 18
Source File: NameImpl.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 4 votes |
public static Name copyElementName(Element element) { String localName = element.getLocalName(); String prefix = element.getPrefix(); String uri = element.getNamespaceURI(); return create(localName, prefix, uri); }
Example 19
Source File: StaxUtil.java From keycloak with Apache License 2.0 | 4 votes |
/** * Write DOM Element to the stream * * @param writer * @param domElement * * @throws ProcessingException */ public static void writeDOMElement(XMLStreamWriter writer, Element domElement) throws ProcessingException { if (registeredNSStack.get() == null) { registeredNSStack.set(new Stack<String>()); } String domElementPrefix = domElement.getPrefix(); if (domElementPrefix == null) { domElementPrefix = ""; } String domElementNS = domElement.getNamespaceURI(); if (domElementNS == null) { domElementNS = ""; } writeStartElement(writer, domElementPrefix, domElement.getLocalName(), domElementNS); // Should we register namespace if (! domElementPrefix.isEmpty() && !registeredNSStack.get().contains(domElementNS)) { // writeNameSpace(writer, domElementPrefix, domElementNS ); registeredNSStack.get().push(domElementNS); } else if (domElementPrefix.isEmpty() && ! domElementNS.isEmpty()) { writeNameSpace(writer, "xmlns", domElementNS); } // Deal with Attributes NamedNodeMap attrs = domElement.getAttributes(); for (int i = 0, len = attrs.getLength(); i < len; ++i) { Attr attr = (Attr) attrs.item(i); String attributePrefix = attr.getPrefix(); String attribLocalName = attr.getLocalName(); String attribValue = attr.getValue(); if (attributePrefix == null || attributePrefix.length() == 0) { if (!("xmlns".equals(attribLocalName))) { writeAttribute(writer, attribLocalName, attribValue); } } else { if ("xmlns".equals(attributePrefix)) { writeNameSpace(writer, attribLocalName, attribValue); } else { writeAttribute(writer, new QName(attr.getNamespaceURI(), attribLocalName, attributePrefix), attribValue); } } } for (Node child = domElement.getFirstChild(); child != null; child = child.getNextSibling()) { writeDOMNode(writer, child); } writeEndElement(writer); }
Example 20
Source File: NameImpl.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static Name copyElementName(Element element) { String localName = element.getLocalName(); String prefix = element.getPrefix(); String uri = element.getNamespaceURI(); return create(localName, prefix, uri); }