Java Code Examples for org.w3c.dom.Document#createDocumentFragment()
The following examples show how to use
org.w3c.dom.Document#createDocumentFragment() .
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: DTMManagerDefault.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
/** * Method createDocumentFragment * * * NEEDSDOC (createDocumentFragment) @return */ synchronized public DTM createDocumentFragment() { try { DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism()); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Node df = doc.createDocumentFragment(); return getDTM(new DOMSource(df), true, null, false, false); } catch (Exception e) { throw new DTMException(e); } }
Example 2
Source File: StaxSerializer.java From cxf with Apache License 2.0 | 6 votes |
private Node appendNewChild(XMLStreamReader reader, boolean wrapped, Document contextDocument, XMLStreamWriter writer, Element element) throws XMLStreamException { StaxUtils.copy(reader, writer); DocumentFragment result = contextDocument.createDocumentFragment(); Node child = element.getFirstChild(); if (wrapped) { child = child.getFirstChild(); } if (child != null && child.getNextSibling() == null) { return child; } while (child != null) { Node nextChild = child.getNextSibling(); result.appendChild(child); child = nextChild; } return result; }
Example 3
Source File: DTMManagerDefault.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Method createDocumentFragment * * * NEEDSDOC (createDocumentFragment) @return */ synchronized public DTM createDocumentFragment() { try { DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism()); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Node df = doc.createDocumentFragment(); return getDTM(new DOMSource(df), true, null, false, false); } catch (Exception e) { throw new DTMException(e); } }
Example 4
Source File: DTMManagerDefault.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * Method createDocumentFragment * * * NEEDSDOC (createDocumentFragment) @return */ synchronized public DTM createDocumentFragment() { try { DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(super.overrideDefaultParser()); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Node df = doc.createDocumentFragment(); return getDTM(new DOMSource(df), true, null, false, false); } catch (Exception e) { throw new DTMException(e); } }
Example 5
Source File: DTMManagerDefault.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * Method createDocumentFragment * * * NEEDSDOC (createDocumentFragment) @return */ synchronized public DTM createDocumentFragment() { try { DocumentBuilderFactory dbf = FactoryImpl.getDOMFactory(super.useServicesMechnism()); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Node df = doc.createDocumentFragment(); return getDTM(new DOMSource(df), true, null, false, false); } catch (Exception e) { throw new DTMException(e); } }
Example 6
Source File: DTMManagerDefault.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * Method createDocumentFragment * * * NEEDSDOC (createDocumentFragment) @return */ synchronized public DTM createDocumentFragment() { try { DocumentBuilderFactory dbf = JdkXmlUtils.getDOMFactory(super.overrideDefaultParser()); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Node df = doc.createDocumentFragment(); return getDTM(new DOMSource(df), true, null, false, false); } catch (Exception e) { throw new DTMException(e); } }
Example 7
Source File: Decrypter.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Parse the specified input stream in a DOM DocumentFragment, owned by the specified Document. * * @param input the InputStream to parse * @param owningDocument the Document which will own the returned DocumentFragment * @return a DocumentFragment * @throws DecryptionException thrown if there is an error parsing the input stream */ private DocumentFragment parseInputStream(InputStream input, Document owningDocument) throws DecryptionException { // Since Xerces currently seems not to handle parsing into a DocumentFragment // without a bit hackery, use this to simulate, so we can keep the API // the way it hopefully will look in the future. Obviously this only works for // input streams containing valid XML instances, not fragments. Document newDocument = null; try { newDocument = parserPool.parse(input); } catch (XMLParserException e) { log.error("Error parsing decrypted input stream", e); throw new DecryptionException("Error parsing input stream", e); } Element element = newDocument.getDocumentElement(); owningDocument.adoptNode(element); DocumentFragment container = owningDocument.createDocumentFragment(); container.appendChild(element); return container; }
Example 8
Source File: NodeTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private DocumentFragment createTestDocumentFragment(Document document) { DocumentFragment docFragment = document.createDocumentFragment(); Element elem = document.createElement("dfElement"); elem.appendChild(document.createTextNode("Text in it")); docFragment.appendChild(elem); return docFragment; }
Example 9
Source File: AbstractElementStack.java From caja with Apache License 2.0 | 5 votes |
/** * @param needsDebugData see {@link DomParser#setNeedsDebugData(boolean)} */ AbstractElementStack(Document doc, Namespaces ns, boolean needsDebugData) { this.doc = doc; this.needsDebugData = needsDebugData; this.rootElement = doc.createDocumentFragment(); this.openNodes.add(new OpenNode(rootElement, ns, null)); }
Example 10
Source File: DocumentFragmentTest.java From anno4j with Apache License 2.0 | 5 votes |
public void testAddNamespaceElement() throws Exception { String xml = "<a:Box xmlns:a=\"http://example.org/a#\" required=\"true\"><a:widget size=\"10\"> </a:widget><a:grommit id=\"23\"> text </a:grommit></a:Box>"; Document doc = parse(xml); ObjectFactory of = con.getObjectFactory(); Entity entity = con.addDesignation(of.createObject(), Entity.class); DocumentFragment frag = doc.createDocumentFragment(); frag.appendChild(doc.getDocumentElement()); entity.setXML(frag); RepositoryResult<Statement> resuts = con.getStatements(entity.getResource(), pred, null); String label = resuts.next().getObject().stringValue(); resuts.close(); assertEquals(xml, label); }
Example 11
Source File: NodeCreateRule.java From lams with GNU General Public License v2.0 | 5 votes |
/** * When this method fires, the digester is told to forward all SAX * ContentHandler events to the builder object, resulting in a DOM being * built instead of normal digester rule-handling occurring. When the * end of the current xml element is encountered, the original content * handler is restored (expected to be NULL, allowing normal Digester * operations to continue). * * @param namespaceURI the namespace URI of the matching element, or an * empty string if the parser is not namespace aware or the element has * no namespace * @param name the local name if the parser is namespace aware, or just * the element name otherwise * @param attributes The attribute list of this element * @throws Exception indicates a JAXP configuration problem */ @Override public void begin(String namespaceURI, String name, Attributes attributes) throws Exception { Document doc = documentBuilder.newDocument(); NodeBuilder builder = null; if (nodeType == Node.ELEMENT_NODE) { Element element = null; if (getDigester().getNamespaceAware()) { element = doc.createElementNS(namespaceURI, name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttributeNS(attributes.getURI(i), attributes.getQName(i), attributes.getValue(i)); } } else { element = doc.createElement(name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttribute(attributes.getQName(i), attributes.getValue(i)); } } builder = new NodeBuilder(doc, element); } else { builder = new NodeBuilder(doc, doc.createDocumentFragment()); } // the NodeBuilder constructor has already saved the original // value of the digester's custom content handler (expected to // be null, but we save it just in case). So now we just // need to tell the digester to forward events to the builder. getDigester().setCustomContentHandler(builder); }
Example 12
Source File: NodeCreateRule.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * Implemented to replace the content handler currently in use by a * NodeBuilder. * * @param namespaceURI the namespace URI of the matching element, or an * empty string if the parser is not namespace aware or the element has * no namespace * @param name the local name if the parser is namespace aware, or just * the element name otherwise * @param attributes The attribute list of this element * @throws Exception indicates a JAXP configuration problem */ @Override public void begin(String namespaceURI, String name, Attributes attributes) throws Exception { XMLReader xmlReader = getDigester().getXMLReader(); Document doc = documentBuilder.newDocument(); NodeBuilder builder = null; if (nodeType == Node.ELEMENT_NODE) { Element element = null; if (getDigester().getNamespaceAware()) { element = doc.createElementNS(namespaceURI, name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttributeNS(attributes.getURI(i), attributes.getLocalName(i), attributes.getValue(i)); } } else { element = doc.createElement(name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttribute(attributes.getQName(i), attributes.getValue(i)); } } builder = new NodeBuilder(doc, element); } else { builder = new NodeBuilder(doc, doc.createDocumentFragment()); } xmlReader.setContentHandler(builder); }
Example 13
Source File: DocumentFragmentBuilder.java From exificient with MIT License | 5 votes |
public DocumentFragment parse(InputStream is) throws SAXException, IOException { // Wrap the fragment in an arbitrary element ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write("<fragment>".getBytes()); int b; while ((b = is.read()) != -1) { baos.write(b); } baos.write("</fragment>".getBytes()); // parse Document doc = this.docBuilder.parse(new ByteArrayInputStream(baos .toByteArray())); // // Import the nodes of the new document into doc so that they // // will be compatible with doc Node node = doc.importNode(doc.getDocumentElement(), true); // Create the document fragment node to hold the new nodes DocumentFragment docfrag = doc.createDocumentFragment(); // Move the nodes into the fragment while (node.hasChildNodes()) { docfrag.appendChild(node.removeChild(node.getFirstChild())); } // Return the fragment return docfrag; }
Example 14
Source File: NodeCreateRule.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * Implemented to replace the content handler currently in use by a * NodeBuilder. * * @param namespaceURI the namespace URI of the matching element, or an * empty string if the parser is not namespace aware or the element has * no namespace * @param name the local name if the parser is namespace aware, or just * the element name otherwise * @param attributes The attribute list of this element * @throws Exception indicates a JAXP configuration problem */ @Override public void begin(String namespaceURI, String name, Attributes attributes) throws Exception { XMLReader xmlReader = getDigester().getXMLReader(); Document doc = documentBuilder.newDocument(); NodeBuilder builder = null; if (nodeType == Node.ELEMENT_NODE) { Element element = null; if (getDigester().getNamespaceAware()) { element = doc.createElementNS(namespaceURI, name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttributeNS(attributes.getURI(i), attributes.getLocalName(i), attributes.getValue(i)); } } else { element = doc.createElement(name); for (int i = 0; i < attributes.getLength(); i++) { element.setAttribute(attributes.getQName(i), attributes.getValue(i)); } } builder = new NodeBuilder(doc, element); } else { builder = new NodeBuilder(doc, doc.createDocumentFragment()); } xmlReader.setContentHandler(builder); }
Example 15
Source File: LogicalHandlerOutInterceptor.java From cxf with Apache License 2.0 | 5 votes |
public void handleMessage(Message message) throws Fault { if (binding.getHandlerChain().isEmpty()) { return; } HandlerChainInvoker invoker = getInvoker(message); if (invoker.getLogicalHandlers().isEmpty()) { return; } XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class); Node nd = message.getContent(Node.class); SOAPMessage m = message.getContent(SOAPMessage.class); Document document = null; if (m != null) { document = m.getSOAPPart(); } else if (nd != null) { document = nd.getOwnerDocument(); } else { document = DOMUtils.newDocument(); message.setContent(Node.class, document); } W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document.createDocumentFragment()); // Replace stax writer with DomStreamWriter message.setContent(XMLStreamWriter.class, writer); message.put(ORIGINAL_WRITER, origWriter); message.getInterceptorChain().add(ending); }
Example 16
Source File: FragmentBuilder.java From regxmllib with BSD 2-Clause "Simplified" License | 5 votes |
/** * Creates a RegXML Fragment, represented an XML DOM Document Fragment * * @param group KLV Group for which the Fragment will be generated. * @param document Document from which the XML DOM Document Fragment will be * created. * * @return XML DOM Document Fragment containing a single RegXML Fragment * * @throws KLVException * @throws com.sandflow.smpte.regxml.FragmentBuilder.RuleException */ public DocumentFragment fromTriplet(Group group, Document document) throws KLVException, RuleException { DocumentFragment df = document.createDocumentFragment(); applyRule3(df, group); /* NOTE: Hack to clean-up namespace prefixes */ for (Map.Entry<URI, String> entry : nsprefixes.entrySet()) { ((Element) df.getFirstChild()).setAttributeNS(XMLNS_NS, "xmlns:" + entry.getValue(), entry.getKey().toString()); } return df; }
Example 17
Source File: Extensions.java From Bytecoder with Apache License 2.0 | 4 votes |
/** * This method is an extension that implements as a Xalan extension * the node-set function also found in xt and saxon. * If the argument is a Result Tree Fragment, then <code>nodeset</code> * returns a node-set consisting of a single root node as described in * section 11.1 of the XSLT 1.0 Recommendation. If the argument is a * node-set, <code>nodeset</code> returns a node-set. If the argument * is a string, number, or boolean, then <code>nodeset</code> returns * a node-set consisting of a single root node with a single text node * child that is the result of calling the XPath string() function on the * passed parameter. If the argument is anything else, then a node-set * is returned consisting of a single root node with a single text node * child that is the result of calling the java <code>toString()</code> * method on the passed argument. * Most of the * actual work here is done in <code>MethodResolver</code> and * <code>XRTreeFrag</code>. * @param myProcessor Context passed by the extension processor * @param rtf Argument in the stylesheet to the nodeset extension function * * NEEDSDOC ($objectName$) @return */ public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf) { String textNodeValue; if (rtf instanceof NodeIterator) { return new NodeSet((NodeIterator) rtf); } else { if (rtf instanceof String) { textNodeValue = (String) rtf; } else if (rtf instanceof Boolean) { textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str(); } else if (rtf instanceof Double) { textNodeValue = new XNumber(((Double) rtf).doubleValue()).str(); } else { textNodeValue = rtf.toString(); } // This no longer will work right since the DTM. // Document myDoc = myProcessor.getContextNode().getOwnerDocument(); Document myDoc = JdkXmlUtils.getDOMDocument(); Text textNode = myDoc.createTextNode(textNodeValue); DocumentFragment docFrag = myDoc.createDocumentFragment(); docFrag.appendChild(textNode); return new NodeSet(docFrag); } }
Example 18
Source File: Extensions.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
/** * This method is an extension that implements as a Xalan extension * the node-set function also found in xt and saxon. * If the argument is a Result Tree Fragment, then <code>nodeset</code> * returns a node-set consisting of a single root node as described in * section 11.1 of the XSLT 1.0 Recommendation. If the argument is a * node-set, <code>nodeset</code> returns a node-set. If the argument * is a string, number, or boolean, then <code>nodeset</code> returns * a node-set consisting of a single root node with a single text node * child that is the result of calling the XPath string() function on the * passed parameter. If the argument is anything else, then a node-set * is returned consisting of a single root node with a single text node * child that is the result of calling the java <code>toString()</code> * method on the passed argument. * Most of the * actual work here is done in <code>MethodResolver</code> and * <code>XRTreeFrag</code>. * @param myProcessor Context passed by the extension processor * @param rtf Argument in the stylesheet to the nodeset extension function * * NEEDSDOC ($objectName$) @return */ public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf) { String textNodeValue; if (rtf instanceof NodeIterator) { return new NodeSet((NodeIterator) rtf); } else { if (rtf instanceof String) { textNodeValue = (String) rtf; } else if (rtf instanceof Boolean) { textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str(); } else if (rtf instanceof Double) { textNodeValue = new XNumber(((Double) rtf).doubleValue()).str(); } else { textNodeValue = rtf.toString(); } // This no longer will work right since the DTM. // Document myDoc = myProcessor.getContextNode().getOwnerDocument(); Document myDoc = getDocument(); Text textNode = myDoc.createTextNode(textNodeValue); DocumentFragment docFrag = myDoc.createDocumentFragment(); docFrag.appendChild(textNode); return new NodeSet(docFrag); } }
Example 19
Source File: Extensions.java From openjdk-8-source with GNU General Public License v2.0 | 4 votes |
/** * This method is an extension that implements as a Xalan extension * the node-set function also found in xt and saxon. * If the argument is a Result Tree Fragment, then <code>nodeset</code> * returns a node-set consisting of a single root node as described in * section 11.1 of the XSLT 1.0 Recommendation. If the argument is a * node-set, <code>nodeset</code> returns a node-set. If the argument * is a string, number, or boolean, then <code>nodeset</code> returns * a node-set consisting of a single root node with a single text node * child that is the result of calling the XPath string() function on the * passed parameter. If the argument is anything else, then a node-set * is returned consisting of a single root node with a single text node * child that is the result of calling the java <code>toString()</code> * method on the passed argument. * Most of the * actual work here is done in <code>MethodResolver</code> and * <code>XRTreeFrag</code>. * @param myProcessor Context passed by the extension processor * @param rtf Argument in the stylesheet to the nodeset extension function * * NEEDSDOC ($objectName$) @return */ public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf) { String textNodeValue; if (rtf instanceof NodeIterator) { return new NodeSet((NodeIterator) rtf); } else { if (rtf instanceof String) { textNodeValue = (String) rtf; } else if (rtf instanceof Boolean) { textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str(); } else if (rtf instanceof Double) { textNodeValue = new XNumber(((Double) rtf).doubleValue()).str(); } else { textNodeValue = rtf.toString(); } // This no longer will work right since the DTM. // Document myDoc = myProcessor.getContextNode().getOwnerDocument(); Document myDoc = getDocument(); Text textNode = myDoc.createTextNode(textNodeValue); DocumentFragment docFrag = myDoc.createDocumentFragment(); docFrag.appendChild(textNode); return new NodeSet(docFrag); } }
Example 20
Source File: Extensions.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
/** * This method is an extension that implements as a Xalan extension * the node-set function also found in xt and saxon. * If the argument is a Result Tree Fragment, then <code>nodeset</code> * returns a node-set consisting of a single root node as described in * section 11.1 of the XSLT 1.0 Recommendation. If the argument is a * node-set, <code>nodeset</code> returns a node-set. If the argument * is a string, number, or boolean, then <code>nodeset</code> returns * a node-set consisting of a single root node with a single text node * child that is the result of calling the XPath string() function on the * passed parameter. If the argument is anything else, then a node-set * is returned consisting of a single root node with a single text node * child that is the result of calling the java <code>toString()</code> * method on the passed argument. * Most of the * actual work here is done in <code>MethodResolver</code> and * <code>XRTreeFrag</code>. * @param myProcessor Context passed by the extension processor * @param rtf Argument in the stylesheet to the nodeset extension function * * NEEDSDOC ($objectName$) @return */ public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf) { String textNodeValue; if (rtf instanceof NodeIterator) { return new NodeSet((NodeIterator) rtf); } else { if (rtf instanceof String) { textNodeValue = (String) rtf; } else if (rtf instanceof Boolean) { textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str(); } else if (rtf instanceof Double) { textNodeValue = new XNumber(((Double) rtf).doubleValue()).str(); } else { textNodeValue = rtf.toString(); } // This no longer will work right since the DTM. // Document myDoc = myProcessor.getContextNode().getOwnerDocument(); Document myDoc = JdkXmlUtils.getDOMDocument(); Text textNode = myDoc.createTextNode(textNodeValue); DocumentFragment docFrag = myDoc.createDocumentFragment(); docFrag.appendChild(textNode); return new NodeSet(docFrag); } }