List of usage examples for org.w3c.dom Element getOwnerDocument
public Document getOwnerDocument();
Document
object associated with this node. From source file:org.apache.ode.utils.DOMUtils.java
/** * @param el/*w w w .j av a2s .com*/ */ public static void pancakeNamespaces(Element el) { Map ns = getParentNamespaces(el); Document d = el.getOwnerDocument(); assert d != null; Iterator it = ns.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); String uri = (String) ns.get(key); Attr a = d.createAttributeNS(NS_URI_XMLNS, (key.length() != 0) ? ("xmlns:" + key) : ("xmlns")); a.setValue(uri); el.setAttributeNodeNS(a); } }
From source file:org.apache.ode.utils.DOMUtils.java
public static Document toDOMDocument(Node node) throws TransformerException { // If the node is the document, just cast it if (node instanceof Document) { return (Document) node; // If the node is an element } else if (node instanceof Element) { Element elem = (Element) node; // If this is the root element, return its owner document if (elem.getOwnerDocument().getDocumentElement() == elem) { return elem.getOwnerDocument(); // else, create a new doc and copy the element inside it } else {//w w w. jav a2 s. c o m Document doc = newDocument(); doc.appendChild(doc.importNode(node, true)); return doc; } // other element types are not handled } else { throw new TransformerException("Unable to convert DOM node to a Document"); } }
From source file:org.apache.openaz.xacml.pdp.policy.dom.DOMAttributeAssignmentExpression.java
public static boolean repair(Node nodeAttributeAssignmentExpression) throws DOMStructureException { Element elementAttributeAssignmentExpression = DOMUtil.getElement(nodeAttributeAssignmentExpression); boolean result = false; if (DOMUtil.getFirstChildElement(elementAttributeAssignmentExpression) == null) { /*/*from ww w . ja v a 2 s . c o m*/ * See if we can repair the <AttributeAssignmentExpression * DataType="">string</AttributeAssignmentExpression> pattern */ Identifier identifier = DOMUtil.getIdentifierAttribute(elementAttributeAssignmentExpression, XACML3.ATTRIBUTE_DATATYPE); String textContent = elementAttributeAssignmentExpression.getTextContent(); if (textContent != null) { textContent = textContent.trim(); } if (textContent != null && textContent.length() > 0 && identifier != null) { Element attributeValue = elementAttributeAssignmentExpression.getOwnerDocument() .createElementNS(XACML3.XMLNS, XACML3.ELEMENT_ATTRIBUTEVALUE); attributeValue.setAttribute(XACML3.ATTRIBUTE_DATATYPE, identifier.stringValue()); attributeValue.setTextContent(textContent); logger.warn("Adding a new AttributeValue using the DataType from the AttributeAssignment"); elementAttributeAssignmentExpression.removeAttribute(XACML3.ATTRIBUTE_DATATYPE); while (elementAttributeAssignmentExpression.hasChildNodes()) { elementAttributeAssignmentExpression .removeChild(elementAttributeAssignmentExpression.getFirstChild()); } elementAttributeAssignmentExpression.appendChild(attributeValue); result = true; } else { throw DOMUtil.newMissingElementException(elementAttributeAssignmentExpression, XACML3.XMLNS, XACML3.ELEMENT_EXPRESSION); } } result = DOMUtil.repairIdentifierAttribute(elementAttributeAssignmentExpression, XACML3.ATTRIBUTE_ATTRIBUTEID, logger) || result; return result; }
From source file:org.apache.pdfbox.preflight.Validator_A1b.java
public static void main(String[] args) throws IOException, TransformerException, ParserConfigurationException { if (args.length == 0) { usage();/*from www .j a va 2 s. co m*/ System.exit(1); } // is output xml ? int posFile = 0; boolean outputXml = "xml".equals(args[posFile]); posFile += outputXml ? 1 : 0; // list boolean isGroup = "group".equals(args[posFile]); posFile += isGroup ? 1 : 0; // list boolean isBatch = "batch".equals(args[posFile]); posFile += isBatch ? 1 : 0; if (isGroup || isBatch) { // prepare the list List<File> ftp = listFiles(args[posFile]); int status = 0; if (!outputXml) { // simple list of files for (File file2 : ftp) { status |= runSimple(file2); } System.exit(status); } else { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); XmlResultParser xrp = new XmlResultParser(); if (isGroup) { Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root = document.createElement("preflights"); document.appendChild(root); root.setAttribute("count", String.format("%d", ftp.size())); for (File file : ftp) { Element result = xrp.validate(document, new FileDataSource(file)); root.appendChild(result); } transformer.transform(new DOMSource(document), new StreamResult(new File(args[posFile] + ".preflight.xml"))); } else { // isBatch for (File file : ftp) { Element result = xrp.validate(new FileDataSource(file)); Document document = result.getOwnerDocument(); document.appendChild(result); transformer.transform(new DOMSource(document), new StreamResult(new File(file.getAbsolutePath() + ".preflight.xml"))); } } } } else { if (!outputXml) { // simple validation System.exit(runSimple(new File(args[posFile]))); } else { // generate xml output XmlResultParser xrp = new XmlResultParser(); Element result = xrp.validate(new FileDataSource(args[posFile])); Document document = result.getOwnerDocument(); document.appendChild(result); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(new DOMSource(document), new StreamResult(System.out)); } } }
From source file:org.apache.rahas.test.util.TestUtil.java
/** * TODO we need to move these common code to a new module. Otherwise code will be duplicated. * We cannot use following method from rampart-core as it creates a cyclic dependency. Therefore we have * to live with following./*from www . java 2s . co m*/ * Creates a DOM Document using the SOAP Envelope. * @param env An org.apache.axiom.soap.SOAPEnvelope instance * @return Returns the DOM Document of the given SOAP Envelope. * @throws Exception If an error occurred during conversion. */ public static Document getDocumentFromSOAPEnvelope(SOAPEnvelope env, boolean useDoom) throws WSSecurityException { try { if (env instanceof Element) { Element element = (Element) env; Document document = element.getOwnerDocument(); // For outgoing messages, Axis2 only creates the SOAPEnvelope, but no document. If // the Axiom implementation also supports DOM, then the envelope (seen as a DOM // element) will have an owner document, but the document and the envelope have no // parent-child relationship. On the other hand, the input expected by WSS4J is // a document with the envelope as document element. Therefore we need to set the // envelope as document element on the owner document. if (element.getParentNode() != document) { document.appendChild(element); } // If the Axiom implementation supports DOM, then it is possible/likely that the // DOM API was used to create the object model (or parts of it). In this case, the // object model is not necessarily well formed with respect to namespaces because // DOM doesn't generate namespace declarations automatically. This is an issue // because WSS4J/Santuario expects that all namespace declarations are present. // If this is not the case, then signature values or encryptions will be incorrect. // To avoid this, we normalize the document. Note that if we disable the other // normalizations supported by DOM, this is generally not a heavy operation. // In particular, the Axiom implementation is not required to expand the object // model (including OMSourcedElements) because the Axiom builder is required to // perform namespace repairing, so that no modifications to unexpanded parts of // the message are required. DOMConfiguration domConfig = document.getDomConfig(); domConfig.setParameter("split-cdata-sections", Boolean.FALSE); domConfig.setParameter("well-formed", Boolean.FALSE); domConfig.setParameter("namespaces", Boolean.TRUE); document.normalizeDocument(); return document; } if (useDoom) { env.build(); // Workaround to prevent a bug in AXIOM where // there can be an incomplete OMElement as the first child body OMElement firstElement = env.getBody().getFirstElement(); if (firstElement != null) { firstElement.build(); } //Get processed headers SOAPHeader soapHeader = env.getHeader(); ArrayList processedHeaderQNames = new ArrayList(); if (soapHeader != null) { Iterator headerBlocs = soapHeader.getChildElements(); while (headerBlocs.hasNext()) { SOAPHeaderBlock element = (SOAPHeaderBlock) headerBlocs.next(); if (element.isProcessed()) { processedHeaderQNames.add(element.getQName()); } } } SOAPModelBuilder stAXSOAPModelBuilder = OMXMLBuilderFactory.createStAXSOAPModelBuilder( OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM), env.getXMLStreamReader()); SOAPEnvelope envelope = (stAXSOAPModelBuilder).getSOAPEnvelope(); envelope.getParent().build(); //Set the processed flag of the processed headers SOAPHeader header = envelope.getHeader(); for (Iterator iter = processedHeaderQNames.iterator(); iter.hasNext();) { QName name = (QName) iter.next(); Iterator omKids = header.getChildrenWithName(name); if (omKids.hasNext()) { ((SOAPHeaderBlock) omKids.next()).setProcessed(); } } Element envElem = (Element) envelope; return envElem.getOwnerDocument(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); env.build(); env.serialize(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); return factory.newDocumentBuilder().parse(bais); } } catch (Exception e) { throw new WSSecurityException("Error in converting SOAP Envelope to Document", e); } }
From source file:org.apache.rampart.util.Axis2Util.java
/** * Creates a DOM Document using the SOAP Envelope. * @param env An org.apache.axiom.soap.SOAPEnvelope instance * @return Returns the DOM Document of the given SOAP Envelope. * @throws Exception//from www. j a v a 2 s .c o m */ public static Document getDocumentFromSOAPEnvelope(SOAPEnvelope env, boolean useDoom) throws WSSecurityException { try { if (env instanceof Element) { return ((Element) env).getOwnerDocument(); } if (useDoom) { env.build(); // Workaround to prevent a bug in AXIOM where // there can be an incomplete OMElement as the first child body OMElement firstElement = env.getBody().getFirstElement(); if (firstElement != null) { firstElement.build(); } //Get processed headers SOAPHeader soapHeader = env.getHeader(); ArrayList processedHeaderQNames = new ArrayList(); if (soapHeader != null) { Iterator headerBlocs = soapHeader.getChildElements(); while (headerBlocs.hasNext()) { SOAPHeaderBlock element = (SOAPHeaderBlock) headerBlocs.next(); if (element.isProcessed()) { processedHeaderQNames.add(element.getQName()); } } } // Check the namespace and find SOAP version and factory String nsURI = null; SOAPFactory factory; if (env.getNamespace().getNamespaceURI().equals(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI)) { nsURI = SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI; factory = DOOMAbstractFactory.getSOAP11Factory(); } else { nsURI = SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI; factory = DOOMAbstractFactory.getSOAP12Factory(); } StAXSOAPModelBuilder stAXSOAPModelBuilder = new StAXSOAPModelBuilder(env.getXMLStreamReader(), factory, nsURI); SOAPEnvelope envelope = (stAXSOAPModelBuilder).getSOAPEnvelope(); ((OMNode) envelope.getParent()).build(); //Set the processed flag of the processed headers SOAPHeader header = envelope.getHeader(); for (Iterator iter = processedHeaderQNames.iterator(); iter.hasNext();) { QName name = (QName) iter.next(); Iterator omKids = header.getChildrenWithName(name); if (omKids.hasNext()) { ((SOAPHeaderBlock) omKids.next()).setProcessed(); } } Element envElem = (Element) envelope; return envElem.getOwnerDocument(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); env.build(); env.serialize(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory factory = getSecuredDocumentBuilderFactory(); factory.setNamespaceAware(true); return factory.newDocumentBuilder().parse(bais); } } catch (Exception e) { throw new WSSecurityException("Error in converting SOAP Envelope to Document", e); } }
From source file:org.apache.rampart.util.RampartUtil.java
public static Element appendChildToSecHeader(RampartMessageData rmd, Element elem) { Element secHeaderElem = rmd.getSecHeader().getSecurityHeader(); Node node = secHeaderElem.getOwnerDocument().importNode(elem, true); return (Element) secHeaderElem.appendChild(node); }
From source file:org.apache.rampart.util.RampartUtil.java
public static Element insertSiblingAfter(RampartMessageData rmd, Element child, Element sibling) { if (child == null) { return appendChildToSecHeader(rmd, sibling); } else {//w ww.j a va2 s. c om if (child.getOwnerDocument().equals(sibling.getOwnerDocument())) { if (child.getParentNode() == null && !child.getLocalName().equals("UsernameToken")) { rmd.getSecHeader().getSecurityHeader().appendChild(child); } ((OMElement) child).insertSiblingAfter((OMElement) sibling); return sibling; } else { Element newSib = (Element) child.getOwnerDocument().importNode(sibling, true); ((OMElement) child).insertSiblingAfter((OMElement) newSib); return newSib; } } }
From source file:org.apache.rampart.util.RampartUtil.java
public static Element insertSiblingBefore(RampartMessageData rmd, Element child, Element sibling) { if (child == null) { return appendChildToSecHeader(rmd, sibling); } else {// w w w . ja v a 2 s . c o m if (child.getOwnerDocument().equals(sibling.getOwnerDocument())) { ((OMElement) child).insertSiblingBefore((OMElement) sibling); return sibling; } else { Element newSib = (Element) child.getOwnerDocument().importNode(sibling, true); ((OMElement) child).insertSiblingBefore((OMElement) newSib); return newSib; } } }
From source file:org.apache.rampart.util.RampartUtil.java
private static Element prependSecHeader(RampartMessageData rmd, Element elem) { Element retElem = null;//www . java2 s . c o m Element secHeaderElem = rmd.getSecHeader().getSecurityHeader(); Node node = secHeaderElem.getOwnerDocument().importNode(elem, true); Element firstElem = (Element) secHeaderElem.getFirstChild(); if (firstElem == null) { retElem = (Element) secHeaderElem.appendChild(node); } else { if (firstElem.getOwnerDocument().equals(elem.getOwnerDocument())) { ((OMElement) firstElem).insertSiblingBefore((OMElement) elem); retElem = elem; } else { Element newSib = (Element) firstElem.getOwnerDocument().importNode(elem, true); ((OMElement) firstElem).insertSiblingBefore((OMElement) newSib); retElem = newSib; } } return retElem; }