Here are the examples of the java api class org.w3c.dom.DocumentFragment taken from open source projects.
1. URIResolver#createWSAEPR()
Project: servicemix4-nmr
File: URIResolver.java
File: URIResolver.java
public static DocumentFragment createWSAEPR(String uri) { Document doc; try { doc = DOMUtil.newDocument(); } catch (Exception e) { throw new RuntimeException(e); } DocumentFragment epr = doc.createDocumentFragment(); Element root = doc.createElement("epr"); Element address = doc.createElementNS(WSAddressingConstants.WSA_NAMESPACE_200508, WSAddressingConstants.WSA_PREFIX + ":" + WSAddressingConstants.EL_ADDRESS); Text txt = doc.createTextNode(uri); address.appendChild(txt); root.appendChild(address); epr.appendChild(root); return epr; }
2. URIResolverTest#testCreateEpr()
Project: servicemix4-nmr
File: URIResolverTest.java
File: URIResolverTest.java
public void testCreateEpr() { DocumentFragment df = URIResolver.createWSAEPR("urn:test"); assertNotNull(df); Element e = DOMUtil.getFirstChildElement(df); assertNotNull(e); assertEquals(new QName("epr"), DOMUtil.getQName(e)); e = DOMUtil.getFirstChildElement(e); assertNotNull(e); assertEquals(new QName(WSAddressingConstants.WSA_NAMESPACE_200508, WSAddressingConstants.EL_ADDRESS), DOMUtil.getQName(e)); assertEquals("urn:test", DOMUtil.getElementText(e)); }
3. XObject#rtree()
Project: openjdk
File: XObject.java
File: XObject.java
/** * Cast result object to a result tree fragment. * * @param support XPath context to use for the conversion * * @return the objec as a result tree fragment. */ public DocumentFragment rtree(XPathContext support) { DocumentFragment docFrag = null; int result = rtf(); if (DTM.NULL == result) { DTM frag = support.createDocumentFragment(); // %OPT% frag.appendTextChild(str()); docFrag = (DocumentFragment) frag.getNode(frag.getDocument()); } else { DTM frag = support.getDTM(result); docFrag = (DocumentFragment) frag.getNode(frag.getDocument()); } return docFrag; }
4. MyEndpointReference#toXML()
Project: ode
File: MyEndpointReference.java
File: MyEndpointReference.java
public Document toXML() { Document xml = DOMUtils.newDocument(); // Prefer to use the external endpoint as our EPR, // but if we dont find one, use the internal endpoint. ServiceEndpoint se = _service.getExternalServiceEndpoint(); if (se == null) se = _service.getInternalServiceEndpoint(); Element root = xml.createElementNS(EndpointReference.SERVICE_REF_QNAME.getNamespaceURI(), EndpointReference.SERVICE_REF_QNAME.getLocalPart()); xml.appendChild(root); // TODO: handle the operation name problem. DocumentFragment fragment = se.getAsReference(null); root.appendChild(xml.importNode(fragment, true)); return xml; }
5. EndpointReferenceContextImpl#convertEndpoint()
Project: ode
File: EndpointReferenceContextImpl.java
File: EndpointReferenceContextImpl.java
public EndpointReference convertEndpoint(QName eprType, Element epr) { Document doc = DOMUtils.newDocument(); DocumentFragment fragment = doc.createDocumentFragment(); NodeList children = epr.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) fragment.appendChild(doc.importNode(children.item(i), true)); ServiceEndpoint se = _ode.getContext().resolveEndpointReference(fragment); if (se == null) return null; return new JbiEndpointReference(se, eprType); }
6. EndpointReferenceContextImpl#resolveEndpointReference()
Project: ode
File: EndpointReferenceContextImpl.java
File: EndpointReferenceContextImpl.java
public EndpointReference resolveEndpointReference(Element epr) { QName elname = new QName(epr.getNamespaceURI(), epr.getLocalName()); if (__log.isDebugEnabled()) { __log.debug("resolveEndpointReference:\n" + prettyPrint(epr)); } if (!elname.equals(EndpointReference.SERVICE_REF_QNAME)) throw new IllegalArgumentException("EPR root element " + elname + " should be " + EndpointReference.SERVICE_REF_QNAME); Document doc = DOMUtils.newDocument(); DocumentFragment fragment = doc.createDocumentFragment(); NodeList children = epr.getChildNodes(); for (int i = 0; i < children.getLength(); ++i) if (children.item(i) instanceof Element) fragment.appendChild(doc.importNode(children.item(i), true)); ServiceEndpoint se = _ode.getContext().resolveEndpointReference(fragment); if (se == null) return null; return new JbiEndpointReference(se); }
7. JOOX#$()
Project: jOOX
File: JOOX.java
File: JOOX.java
/** * Create a new DOM element in an independent document */ public static Match $(String name) { Document document = builder().newDocument(); DocumentFragment fragment = Util.createContent(document, name); if (fragment != null) { document.appendChild(fragment); } else { document.appendChild(document.createElement(name)); } return $(document); }
8. XObject#rtree()
Project: j2objc
File: XObject.java
File: XObject.java
/** * Cast result object to a result tree fragment. * * @param support XPath context to use for the conversion * * @return the objec as a result tree fragment. */ public DocumentFragment rtree(XPathContext support) { DocumentFragment docFrag = null; int result = rtf(); if (DTM.NULL == result) { DTM frag = support.createDocumentFragment(); // %OPT% frag.appendTextChild(str()); docFrag = (DocumentFragment) frag.getNode(frag.getDocument()); } else { DTM frag = support.getDTM(result); docFrag = (DocumentFragment) frag.getNode(frag.getDocument()); } return docFrag; }
9. SimpleSessionContext#saveXML()
Project: cocoon
File: SimpleSessionContext.java
File: SimpleSessionContext.java
/** * Try to save XML from the context. * If the context does not provide the ability of saving, * an exception is thrown. */ public void saveXML(String path, SourceParameters parameters) throws SAXException, ProcessingException, IOException { if (this.saveResource == null) { throw new ProcessingException("The context " + this.name + " does not support saving."); } DocumentFragment frag = this.getXML(path); if (frag == null) { // create empty fake document frag = DOMUtil.createDocument().createDocumentFragment(); } XMLUtil.writeDOM(this.saveResource, null, parameters, frag, this.resolver, "xml"); }
10. SessionContextImpl#streamXML()
Project: cocoon
File: SessionContextImpl.java
File: SessionContextImpl.java
/** * Stream the XML directly to the handler. This streams the contents of getXML() * to the given handler without creating a DocumentFragment containing a copy * of the data */ public synchronized boolean streamXML(String path, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws SAXException, ProcessingException { boolean streamed = false; DocumentFragment fragment = this.getXML(path); if (fragment != null) { streamed = true; IncludeXMLConsumer.includeNode(fragment, contentHandler, lexicalHandler); } return streamed; }
11. PortalToolManager#sSet()
Project: cocoon
File: PortalToolManager.java
File: PortalToolManager.java
/** * Sets a value in the auth context * @param key Path (e.g. /foo/bar) * @param value Value */ public void sSet(String key, String value) { SessionContext ctx; try { ctx = this.contextManager.getContext("authentication"); } catch (Exception e) { return; } if (!key.startsWith("/")) key = "/" + key; DocumentFragment frag; try { frag = ctx.getXML("/"); org.w3c.dom.Document doc = frag.getOwnerDocument(); DocumentFragment newFrag = doc.createDocumentFragment(); Text txt = doc.createTextNode(value); newFrag.appendChild(txt); ctx.setXML("/portalTools" + key, newFrag); } catch (ProcessingException e) { } }
12. PortalToolManager#sGet()
Project: cocoon
File: PortalToolManager.java
File: PortalToolManager.java
/** * Returns a value from the auth context * @param key Path (e.g. /foo/bar) */ public String sGet(String key) { SessionContext ctx; try { ctx = this.contextManager.getContext("authentication"); } catch (Exception e) { return null; } if (!key.startsWith("/")) key = "/" + key; DocumentFragment node = null; try { node = ctx.getXML("/portalTools" + key); } catch (Exception e) { } return org.apache.cocoon.xml.dom.DOMUtil.getValueOfNode(node); }
13. NodesTest#testRenderModes()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderModes() throws Exception { DocumentFragment f = htmlFragment(fromString("<input checked name=foo type=checkbox>")); assertEquals("<input checked=\"\" name=\"foo\" type=\"checkbox\" />", Nodes.render(f, MarkupRenderMode.XML)); assertEquals("<input checked=\"\" name=\"foo\" type=\"checkbox\" />", Nodes.render(f, MarkupRenderMode.HTML)); assertEquals("<input checked name=\"foo\" type=\"checkbox\" />", Nodes.render(f, MarkupRenderMode.HTML4_BACKWARDS_COMPAT)); }
14. NodesTest#testRenderWithMaskedOutputNamespace1()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderWithMaskedOutputNamespace1() throws Exception { DocumentFragment fragment = xmlFragment(fromString("<svg:foo><xml:bar/></svg:foo>")); Namespaces ns = new Namespaces(Namespaces.HTML_DEFAULT, "svg", Namespaces.XML_NAMESPACE_URI); StringBuilder sb = new StringBuilder(); RenderContext rc = new RenderContext(new Concatenator(sb)).withMarkupRenderMode(MarkupRenderMode.XML); Nodes.render(fragment, ns, rc); rc.getOut().noMoreTokens(); assertEquals("" + "<_ns2:foo xmlns:_ns2=\"http://www.w3.org/2000/svg\">" + "<svg:bar></svg:bar></_ns2:foo>", sb.toString()); }
15. NodesTest#testRenderWithUnknownNamespace()
Project: caja
File: NodesTest.java
File: NodesTest.java
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)); }
16. DomParserTest#testIssueOSTemplateMarkup()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testIssueOSTemplateMarkup() throws Exception { String data = "" + "<div xmlns:osx=\"http://ns.opensocial.org/2008/extensions\">\n" + "<osx:NavigateToApp>\n" + "<img border=\"0\" title=\"Justin\" src=\"foo.gif\">\n" + "</osx:NavigateToApp>\n" + // throw a IllegalStateException "<osx:NavigateToApp>\n" + "<span class=\"profile-link\">foo</span>" + "</osx:NavigateToApp>" + "</div>"; DocumentFragment f = htmlFragment(fromString(data)); assertEquals("" + "<div xmlns:osx=\"http://ns.opensocial.org/2008/extensions\">\n" + "<osx:NavigateToApp>\n" + "<img border=\"0\" src=\"foo.gif\" title=\"Justin\" />\n" + "</osx:NavigateToApp>\n" + "<osx:NavigateToApp>\n" + "<span class=\"profile-link\">foo</span>" + "</osx:NavigateToApp>" + "</div>", Nodes.render(f)); }
17. DomParserTest#testIssue1211WithPrefixNs()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testIssue1211WithPrefixNs() throws Exception { DocumentFragment f = htmlFragment(fromString("" + "<div xmlns:f=\"http://ns.opensocial.org/2008/markup\">\n" + " <f:foo bar=\"baz\" f:boo=\"far\"/>\n" + "</div>")); assertEquals("" + "<div xmlns:f=\"http://ns.opensocial.org/2008/markup\">\n" + " <f:foo bar=\"baz\" boo=\"far\">\n" + "</f:foo></div>", Nodes.render(f)); }
18. DomParserTest#testIssue1211XmlnsOnDiv()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testIssue1211XmlnsOnDiv() throws Exception { DocumentFragment f = htmlFragment(fromString("" + "<div xmlns:os=\"http://ns.opensocial.org/2008/markup\">\n" + " <os:ViewerRequest key=\"viewer\"/>\n" + "</div>")); assertEquals("" + "<div xmlns:os=\"http://ns.opensocial.org/2008/markup\">\n" + " <os:ViewerRequest key=\"viewer\">\n" + "</os:ViewerRequest></div>", Nodes.render(f)); }
19. DomParserTest#testIssue1211DefaultXmlnsOnScript()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testIssue1211DefaultXmlnsOnScript() throws Exception { DocumentFragment f = htmlFragment(fromString("" + "<script type=\"text/os-data\"\n" + " xmlns=\"http://ns.opensocial.org/2008/markup\">\n" + " <ViewerRequest key=\"viewer\"/>\n" + "</script>")); assertEquals("" + "<script type=\"text/os-data\">\n" + " <ViewerRequest key=\"viewer\"/>\n" + "</script>", Nodes.render(f)); }
20. DomParserTest#testIssue1211XmlnsOnScript()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testIssue1211XmlnsOnScript() throws Exception { DocumentFragment f = htmlFragment(fromString("" + "<script type=\"text/os-data\"\n" + " xmlns:os=\"http://ns.opensocial.org/2008/markup\">\n" + " <os:ViewerRequest key=\"viewer\"/>\n" + "</script>")); assertEquals("" + "<script type=\"text/os-data\"" + " xmlns:os=\"http://ns.opensocial.org/2008/markup\">\n" + " <os:ViewerRequest key=\"viewer\"/>\n" + "</script>", Nodes.render(f)); }
21. DomParserTest#testUnrenderableXMLTree3()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testUnrenderableXMLTree3() throws Exception { DocumentFragment t = xmlFragment(fromString("<xmp> </XM<!-- -->P> </xmp>")); assertEquals("<xmp> </XMP> </xmp>", Nodes.render(t, MarkupRenderMode.XML)); try { String badness = Nodes.render(t, MarkupRenderMode.HTML); fail("Bad HTML rendered: " + badness); } catch (UncheckedUnrenderableException ex) { } }
22. DomParserTest#testUnrenderableXMLTree2()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testUnrenderableXMLTree2() throws Exception { DocumentFragment t = xmlFragment(fromString("<xmp><![CDATA[ </xM]]>p </xmp>")); assertEquals("<xmp> </xMp </xmp>", Nodes.render(t, MarkupRenderMode.XML)); try { String badness = Nodes.render(t, MarkupRenderMode.HTML); fail("Bad HTML rendered: " + badness); } catch (UncheckedUnrenderableException ex) { } }
23. DomParserTest#testUnrenderableXMLTree1()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testUnrenderableXMLTree1() throws Exception { DocumentFragment t = xmlFragment(fromString("<xmp><![CDATA[ </xmp> ]]></xmp>")); assertEquals("<xmp> </xmp> </xmp>", Nodes.render(t, MarkupRenderMode.XML)); try { String badness = Nodes.render(t, MarkupRenderMode.HTML); fail("Bad HTML rendered: " + badness); } catch (UncheckedUnrenderableException ex) { } }
24. DomParser#parseFragment()
Project: caja
File: DomParser.java
File: DomParser.java
/** * Parses a snippet of markup creating new nodes using the given document. * The snippet need not be a single element as in an XML or HTML document. * For HTML, this will create no implied HTML, HEAD, or BODY elements. * Any doctype on the input will be ignored, and that on the input document * used instead. */ public DocumentFragment parseFragment(Document doc) throws ParseException { OpenElementStack elementStack = makeElementStack(doc, mq); // Make sure the elementStack is empty. elementStack.open(true); skipFragmentIgnorables(); while (!tokens.isEmpty()) { parseDom(elementStack); skipFragmentIgnorables(); } checkEnd(elementStack); DocumentFragment fragment = elementStack.getRootElement(); if (elementStack.needsNamespaceFixup()) { fragment = (DocumentFragment) fixup(fragment, ns); } return fragment; }
25. WdcParser#parseTagSoup()
Project: anthelion
File: WdcParser.java
File: WdcParser.java
private DocumentFragment parseTagSoup(InputSource input) throws Exception { HTMLDocumentImpl doc = new HTMLDocumentImpl(); DocumentFragment frag = doc.createDocumentFragment(); DOMBuilder builder = new DOMBuilder(doc, frag); org.ccil.cowan.tagsoup.Parser reader = new org.ccil.cowan.tagsoup.Parser(); reader.setContentHandler(builder); reader.setFeature(org.ccil.cowan.tagsoup.Parser.ignoreBogonsFeature, true); reader.setFeature(org.ccil.cowan.tagsoup.Parser.bogonsEmptyFeature, false); reader.setProperty("http://xml.org/sax/properties/lexical-handler", builder); reader.parse(input); return frag; }
26. PortalManagerImpl#buildBaseProfile()
Project: cocoon
File: PortalManagerImpl.java
File: PortalManagerImpl.java
/** * Get the base profile for the current application. * The base profile consists of the layout and the coplet profile * and optional the type profile */ private DocumentFragment buildBaseProfile(Map config, boolean adminProfile) throws ProcessingException { // calling method is synced if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("BEGIN buildBaseProfile config=" + config + ", adminProfile=" + adminProfile); } DocumentFragment copletsFragment; DocumentFragment layoutFragment; DocumentFragment typeFragment; DocumentFragment profile; Document profileDoc; Element profileRoot; String res; SourceParameters pars = new SourceParameters(); RequestState reqstate = this.getRequestState(); pars.setSingleParameterValue("application", reqstate.getApplicationName()); pars.setSingleParameterValue("handler", reqstate.getHandlerName()); pars.setSingleParameterValue("profile", "coplet-base"); // First load the base profiles: copletProfile + layoutProfile res = (String) config.get(PortalConstants.CONF_COPLETBASE_RESOURCE); if (res == null) { throw new ProcessingException("No configuration for portal-coplet base profile found."); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("Loading coplet base profile"); } copletsFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("coplet base profile loaded"); } res = (String) config.get(PortalConstants.CONF_LAYOUTBASE_RESOURCE); if (res == null) { throw new ProcessingException("No configuration for portal-layout base profile found."); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading layout base profile"); } pars.setSingleParameterValue("profile", "layout-base"); layoutFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("layout base profile loaded"); } // now create the base profile containing the above profiles profileDoc = DOMUtil.createDocument(); profile = profileDoc.createDocumentFragment(); profileRoot = profileDoc.createElementNS(null, "profile"); profile.appendChild(profileRoot); profileRoot.appendChild(profileDoc.importNode(DOMUtil.selectSingleNode(layoutFragment, "layout-profile", this.xpathProcessor), true)); profileRoot.appendChild(profileDoc.importNode(DOMUtil.selectSingleNode(copletsFragment, "coplets-profile", this.xpathProcessor), true)); // if avalailable append the type profile if (adminProfile) { res = (String) config.get(PortalConstants.CONF_ADMIN_TYPE_BASE); pars.setSingleParameterValue("profile", "admin-type-base"); } else { res = (String) config.get(PortalConstants.CONF_TYPEBASE_RESOURCE); pars.setSingleParameterValue("profile", "type-base"); } if (res != null) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading type base profile"); } typeFragment = SourceUtil.readDOM(res, null, pars, this.resolver); profileRoot.appendChild(profileDoc.importNode(DOMUtil.selectSingleNode(typeFragment, "type-profile", this.xpathProcessor), true)); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("type base profile loaded"); } } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END buildBaseProfile profile=" + profile); } return profile; }
27. PortalManagerImpl#buildProfileDelta()
Project: cocoon
File: PortalManagerImpl.java
File: PortalManagerImpl.java
/** * Build the profile delta */ private DocumentFragment buildProfileDelta(String type, String role, String id, boolean adminProfile) throws SAXException, ProcessingException, IOException, javax.xml.transform.TransformerException { // calling method must be synchronized if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END buildProfileDeltaN type=" + type + ", role=" + role + ", id=" + id); } Map originalProfile; Map baseProfile; String baseType, baseRole, baseID, rootElementName; DocumentFragment originalFragment; DocumentFragment delta; SessionContext context = this.getContext(true); originalProfile = this.retrieveProfile(this.getProfileID(type, role, id, adminProfile)); if (originalProfile == null) { throw new ProcessingException("buildProfileDelta: no profile found for " + type + " - " + role + " - " + id + "."); } if (type.equals(PortalManagerImpl.BUILDTYPE_VALUE_ID)) { baseType = PortalManagerImpl.BUILDTYPE_VALUE_ROLE; baseRole = role; baseID = null; rootElementName = "user-delta"; } else if (type.equals(PortalManagerImpl.BUILDTYPE_VALUE_ROLE)) { baseType = PortalManagerImpl.BUILDTYPE_VALUE_GLOBAL; baseRole = null; baseID = null; rootElementName = "role-delta"; } else if (type.equals(PortalManagerImpl.BUILDTYPE_VALUE_GLOBAL)) { baseType = PortalManagerImpl.BUILDTYPE_VALUE_BASIC; baseRole = null; baseID = null; rootElementName = "global-delta"; } else { throw new ProcessingException("buildProfileDelta: type '" + type + "' not allowed."); } // the profile is created as we dont want to use any memory representation! this.createProfile(context, baseType, baseRole, baseID, adminProfile); baseProfile = this.retrieveProfile(this.getProfileID(baseType, baseRole, baseID, adminProfile)); if (baseProfile == null) { throw new ProcessingException("buildProfileDelta: no baseProfile found."); } originalFragment = (DocumentFragment) originalProfile.get(PortalConstants.PROFILE_PROFILE); delta = originalFragment.getOwnerDocument().createDocumentFragment(); delta.appendChild(delta.getOwnerDocument().createElementNS(null, rootElementName)); // Copy portal content Node profileDelta = DOMUtil.getFirstNodeFromPath(originalFragment, new String[] { "profile", "portal-profile" }, false).cloneNode(true); delta.getFirstChild().appendChild(profileDelta); // Diff layout profile, coplet profile, personal profile but not status profile! this.diff(originalFragment, (DocumentFragment) baseProfile.get(PortalConstants.PROFILE_PROFILE), "profile/layout-profile", (Element) delta.getFirstChild()); this.diff(originalFragment, (DocumentFragment) baseProfile.get(PortalConstants.PROFILE_PROFILE), "profile/coplets-profile", (Element) delta.getFirstChild()); if (type.equals(PortalManagerImpl.BUILDTYPE_VALUE_GLOBAL)) { profileDelta = DOMUtil.getFirstNodeFromPath(originalFragment, new String[] { "profile", "personal-profile" }, false).cloneNode(true); delta.getFirstChild().appendChild(profileDelta); } else { this.diff(originalFragment, (DocumentFragment) baseProfile.get(PortalConstants.PROFILE_PROFILE), "profile/personal-profile", (Element) delta.getFirstChild()); } // check for the highes coplet number Node[] miscNodes = (Node[]) originalProfile.get(PortalConstants.PROFILE_MISC_POINTER); Element lastCoplet = (Element) miscNodes[PortalConstants.PROFILE_MISC_LAST_COPLET_NODE]; if (lastCoplet != null) { String lastNumber = lastCoplet.getAttributeNS(null, "number"); if (lastNumber != null) { int value = new Integer(lastNumber).intValue(); if (value > 1000000) { NodeList coplets = DOMUtil.selectNodeList(delta, "profile/portal-profile/descendant::coplet[@id and @number]", this.xpathProcessor); if (coplets != null) { Element copletNode; String oldNumber; String copletId; Element statusNode; boolean copletsChanged = false; for (int i = 0; i < coplets.getLength(); i++) { copletNode = (Element) coplets.item(i); oldNumber = copletNode.getAttributeNS(null, "number"); copletId = copletNode.getAttributeNS(null, "id"); statusNode = (Element) DOMUtil.getSingleNode(delta, "status-profile/customization/coplet[@id='" + copletId + "' and @number='" + oldNumber + "']", this.xpathProcessor); copletNode.setAttributeNS(null, "number", "" + (i + 1)); if (statusNode != null) { statusNode.setAttributeNS(null, "number", "" + (i + 1)); copletsChanged = true; } } if (copletsChanged) { this.saveUserStatusProfile(originalProfile, this.getConfiguration(), role, id); } } } } } // Last part: strip type information NodeList typeElements = DOMUtil.selectNodeList(delta, "descendant::*[@formpath and @formdescription and @formtype]", this.xpathProcessor); if (typeElements != null) { for (int i = 0; i < typeElements.getLength(); i++) { ((Element) typeElements.item(i)).removeAttributeNS(null, "formpath"); ((Element) typeElements.item(i)).removeAttributeNS(null, "formdescription"); ((Element) typeElements.item(i)).removeAttributeNS(null, "formtype"); } } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END buildProfileDelta delta=" + delta); } return delta; }
28. WdcParser#parseNeko()
Project: anthelion
File: WdcParser.java
File: WdcParser.java
private DocumentFragment parseNeko(InputSource input) throws Exception { DOMFragmentParser parser = new DOMFragmentParser(); try { parser.setFeature("http://cyberneko.org/html/features/augmentations", true); parser.setProperty("http://cyberneko.org/html/properties/default-encoding", defaultCharEncoding); parser.setFeature("http://cyberneko.org/html/features/scanner/ignore-specified-charset", true); parser.setFeature("http://cyberneko.org/html/features/balance-tags/ignore-outside-content", false); parser.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true); parser.setFeature("http://cyberneko.org/html/features/report-errors", LOG.isTraceEnabled()); } catch (SAXException e) { } // convert Document to DocumentFragment HTMLDocumentImpl doc = new HTMLDocumentImpl(); doc.setErrorChecking(false); DocumentFragment res = doc.createDocumentFragment(); DocumentFragment frag = doc.createDocumentFragment(); parser.parse(input, frag); res.appendChild(frag); try { while (true) { frag = doc.createDocumentFragment(); parser.parse(input, frag); if (!frag.hasChildNodes()) break; if (LOG.isInfoEnabled()) { LOG.info(" - new frag, " + frag.getChildNodes().getLength() + " nodes."); } res.appendChild(frag); } } catch (Exception e) { LOG.error("Error: ", e); } ; return res; }
29. NodeTest#createTestDocumentFragment()
Project: openjdk
File: NodeTest.java
File: NodeTest.java
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; }
30. CajaTestCase#html()
Project: caja
File: CajaTestCase.java
File: CajaTestCase.java
protected DocumentFragment html(CharProducer cp) throws ParseException { Node root = parseMarkup(cp, false, true); DocumentFragment fragment = root.getOwnerDocument().createDocumentFragment(); fragment.appendChild(root); Nodes.setFilePositionFor(fragment, Nodes.getFilePositionFor(root)); return fragment; }
31. Dom#transplant()
Project: caja
File: Dom.java
File: Dom.java
/** * Create a Dom object containing a DocumentFragment containing the provided * node. * * If the provided node is a Document, transplant its root node. If the * provided node is a DocumentFragment, use it rather than creating a new * fragment. */ public static Dom transplant(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { node = ((Document) node).getDocumentElement(); } else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { return new Dom((DocumentFragment) node); } DocumentFragment f = node.getOwnerDocument().createDocumentFragment(); f.appendChild(node); Nodes.setFilePositionFor(f, Nodes.getFilePositionFor(node)); return new Dom(f); }
32. OpenSAMLUtil#toDom()
Project: wss4j
File: OpenSAMLUtil.java
File: OpenSAMLUtil.java
/** * Convert a SAML Assertion from a XMLObject to a DOM Element * * @param xmlObject of type XMLObject * @param doc of type Document * @param signObject whether to sign the XMLObject during marshalling * @return Element * @throws MarshallingException */ public static Element toDom(XMLObject xmlObject, Document doc, boolean signObject) throws WSSecurityException { Marshaller marshaller = marshallerFactory.getMarshaller(xmlObject); Element element = null; DocumentFragment frag = doc == null ? null : doc.createDocumentFragment(); try { if (frag != null) { while (doc.getFirstChild() != null) { frag.appendChild(doc.removeChild(doc.getFirstChild())); } } try { if (doc == null) { element = marshaller.marshall(xmlObject); } else { element = marshaller.marshall(xmlObject, doc); } } catch (MarshallingException ex) { throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, ex, "empty", new Object[] { "Error marshalling a SAML assertion" }); } if (signObject) { signXMLObject(xmlObject); } } finally { if (frag != null) { while (doc.getFirstChild() != null) { doc.removeChild(doc.getFirstChild()); } doc.appendChild(frag); } } return element; }
33. RenderingPipelineIntegrationTest#testRenderingPipeline()
Project: uPortal
File: RenderingPipelineIntegrationTest.java
File: RenderingPipelineIntegrationTest.java
@Test public void testRenderingPipeline() throws Exception { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); final Document doc = builder.newDocument(); final DocumentFragment headFragment = doc.createDocumentFragment(); final MockHttpServletRequest request = new MockHttpServletRequest(); final MockHttpServletResponse response = new MockHttpServletResponse(); final IPortalUrlBuilder portalUrlBuilder = mock(IPortalUrlBuilder.class); final IPortletUrlBuilder portletUrlBuilder = mock(IPortletUrlBuilder.class); when(portalUrlBuilder.getUrlString()).thenReturn("URL_PLACEHOLDER"); when(portletUrlBuilder.getPortalUrlBuilder()).thenReturn(portalUrlBuilder); when(portalUrlBuilder.getTargetedPortletUrlBuilder()).thenReturn(portletUrlBuilder); when(portalUrlBuilder.getPortletUrlBuilder(any(IPortletWindowId.class))).thenReturn(portletUrlBuilder); when(this.resourcesElementsProvider.getResourcesXmlFragment(any(HttpServletRequest.class), eq("/media/skins/respondr/defaultSkin/skin.xml"))).thenReturn(headFragment.getChildNodes()); when(this.portalUrlProvider.getDefaultUrl(any(HttpServletRequest.class))).thenReturn(portalUrlBuilder); when(this.portalUrlProvider.getPortalUrlBuilderByLayoutNode(any(HttpServletRequest.class), any(String.class), any(UrlType.class))).thenReturn(portalUrlBuilder); when(this.portalUrlProvider.getPortalUrlBuilderByPortletFName(any(HttpServletRequest.class), any(String.class), any(UrlType.class))).thenReturn(portalUrlBuilder); final IPortletWindow portletWindow = mock(IPortletWindow.class); when(portletWindowRegistry.getPortletWindow(any(HttpServletRequest.class), any(StartElement.class))).thenReturn(new Tuple<IPortletWindow, StartElement>(portletWindow, null)); when(portletWindowRegistry.getOrCreateDefaultPortletWindowByLayoutNodeId(any(HttpServletRequest.class), any(String.class))).thenReturn(portletWindow); when(portletWindowRegistry.getOrCreateDefaultPortletWindowByFname(any(HttpServletRequest.class), anyString())).thenReturn(portletWindow); when(portletWindow.getPortletWindowId()).thenReturn(new MockPortletWindowId("1")); final PipelineEventReader<?, ?> eventReader = this.component.getEventReader(request, response); for (final Object event : eventReader) { logger.debug(toString(event)); } final String mediaType = eventReader.getOutputProperty(OutputKeys.MEDIA_TYPE); assertEquals("text/html", mediaType); }
34. ParserBolt#execute()
Project: storm-crawler
File: ParserBolt.java
File: ParserBolt.java
@Override public void execute(Tuple tuple) { eventCounter.scope("tuple_in").incrBy(1); byte[] content = tuple.getBinaryByField("content"); String url = tuple.getStringByField("url"); Metadata metadata = (Metadata) tuple.getValueByField("metadata"); long start = System.currentTimeMillis(); ByteArrayInputStream bais = new ByteArrayInputStream(content); org.apache.tika.metadata.Metadata md = new org.apache.tika.metadata.Metadata(); // provide the mime-type as a clue for guessing String httpCT = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE); if (StringUtils.isNotBlank(httpCT)) { // pass content type from server as a clue md.set(org.apache.tika.metadata.Metadata.CONTENT_TYPE, httpCT); } // as well as the filename try { URL _url = new URL(url); md.set(org.apache.tika.metadata.Metadata.RESOURCE_NAME_KEY, _url.getFile()); } catch (MalformedURLException e1) { throw new IllegalStateException("Malformed URL", e1); } LinkContentHandler linkHandler = new LinkContentHandler(); ContentHandler textHandler = new BodyContentHandler(-1); TeeContentHandler teeHandler = new TeeContentHandler(linkHandler, textHandler); ParseContext parseContext = new ParseContext(); try { parseContext.set(HtmlMapper.class, (HtmlMapper) HTMLMapperClass.newInstance()); } catch (Exception e) { LOG.error("Exception while specifying HTMLMapper {}", url, e); } // build a DOM if required by the parseFilters DocumentFragment root = null; if (parseFilters.needsDOM()) { HTMLDocumentImpl doc = new HTMLDocumentImpl(); doc.setErrorChecking(false); root = doc.createDocumentFragment(); DOMBuilder domhandler = new DOMBuilder(doc, root); domhandler.setUpperCaseElementNames(upperCaseElementNames); domhandler.setDefaultNamespaceURI(XHTMLContentHandler.XHTML); teeHandler = new TeeContentHandler(linkHandler, textHandler, domhandler); } // parse String text; try { tika.getParser().parse(bais, teeHandler, md, parseContext); text = textHandler.toString(); } catch (Throwable e) { String errorMessage = "Exception while parsing " + url + ": " + e; LOG.error(errorMessage); metadata.setValue(Constants.STATUS_ERROR_SOURCE, "content parsing"); metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage); collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.ERROR)); collector.ack(tuple); eventCounter.scope("error_content_parsing_" + e.getClass().getSimpleName()).incrBy(1); eventCounter.scope("parse exception").incrBy(1); return; } finally { try { bais.close(); } catch (IOException e) { LOG.error("Exception while closing stream", e); } } // add parse md to metadata for (String k : md.names()) { String[] values = md.getValues(k); metadata.setValues("parse." + k, values); } long duration = System.currentTimeMillis() - start; LOG.info("Parsed {} in {} msec", url, duration); // filter and convert the outlinks List<Outlink> outlinks = toOutlinks(url, linkHandler.getLinks(), metadata); ParseResult parse = new ParseResult(); parse.setOutlinks(outlinks); // parse data of the parent URL ParseData parseData = parse.get(url); parseData.setMetadata(metadata); parseData.setText(text); parseData.setContent(content); // apply the parse filters if any try { parseFilters.filter(url, content, root, parse); } catch (RuntimeException e) { String errorMessage = "Exception while running parse filters on " + url + ": " + e; LOG.error(errorMessage); metadata.setValue(Constants.STATUS_ERROR_SOURCE, "content filtering"); metadata.setValue(Constants.STATUS_ERROR_MESSAGE, errorMessage); collector.emit(StatusStreamName, tuple, new Values(url, metadata, Status.ERROR)); collector.ack(tuple); eventCounter.scope("error_content_filtering_" + e.getClass().getSimpleName()).incrBy(1); eventCounter.scope("parse exception").incrBy(1); return; } if (emitOutlinks) { for (Outlink outlink : parse.getOutlinks()) { collector.emit(StatusStreamName, tuple, new Values(outlink.getTargetURL(), outlink.getMetadata(), Status.DISCOVERED)); } } for (Map.Entry<String, ParseData> doc : parse) { ParseData parseDoc = doc.getValue(); collector.emit(tuple, new Values(doc.getKey(), parseDoc.getContent(), parseDoc.getMetadata(), parseDoc.getText())); } collector.ack(tuple); eventCounter.scope("tuple_success").incrBy(1); }
35. JSoupDOMBuilder#jsoup2HTML()
Project: storm-crawler
File: JSoupDOMBuilder.java
File: JSoupDOMBuilder.java
public static DocumentFragment jsoup2HTML(org.jsoup.nodes.Document jsoupDocument) { HTMLDocumentImpl htmlDoc = new HTMLDocumentImpl(); htmlDoc.setErrorChecking(false); DocumentFragment fragment = htmlDoc.createDocumentFragment(); createDOM(jsoupDocument, fragment, htmlDoc, new HashMap<String, String>()); return fragment; }
36. JSoupParserBolt#execute()
Project: storm-crawler
File: JSoupParserBolt.java
File: JSoupParserBolt.java
@Override public void execute(Tuple tuple) { byte[] content = tuple.getBinaryByField("content"); String url = tuple.getStringByField("url"); Metadata metadata = (Metadata) tuple.getValueByField("metadata"); LOG.info("Parsing : starting {}", url); // check that its content type is HTML // look at value found in HTTP headers boolean CT_OK = false; String mimeType = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE); if (detectMimeType) { mimeType = guessMimeType(url, mimeType, content); // store identified type in md metadata.setValue("parse.Content-Type", mimeType); } if (StringUtils.isNotBlank(mimeType)) { if (mimeType.toLowerCase().contains("html")) { CT_OK = true; } } else // go ahead even if no mimetype is available { CT_OK = true; } if (!CT_OK) { if (this.treat_non_html_as_error) { String errorMessage = "Exception content-type " + mimeType + " for " + url; RuntimeException e = new RuntimeException(errorMessage); handleException(url, e, metadata, tuple, "content-type checking", errorMessage); } else { LOG.info("Incorrect mimetype - passing on : {}", url); collector.emit(tuple, new Values(url, content, metadata, "")); collector.ack(tuple); } return; } long start = System.currentTimeMillis(); String charset = getContentCharset(content, metadata); // get the robots tags from the fetch metadata RobotsTags robotsTags = new RobotsTags(metadata); Map<String, List<String>> slinks; String text = ""; DocumentFragment fragment; try (ByteArrayInputStream bais = new ByteArrayInputStream(content)) { org.jsoup.nodes.Document jsoupDoc = Jsoup.parse(bais, charset, url); fragment = JSoupDOMBuilder.jsoup2HTML(jsoupDoc); // extracts the robots directives from the meta tags robotsTags.extractMetaTags(fragment); // store a normalised representation in metadata // so that the indexer is aware of it robotsTags.normaliseToMetadata(metadata); // and we are in strict mode if (robotsTags.isNoFollow() && robots_noFollow_strict) { slinks = new HashMap<>(0); } else { Elements links = jsoupDoc.select("a[href]"); slinks = new HashMap<>(links.size()); for (Element link : links) { // abs:href tells jsoup to return fully qualified domains // for // relative urls. // e.g.: /foo will resolve to http://shopstyle.com/foo String targetURL = link.attr("abs:href"); // nofollow boolean noFollow = "nofollow".equalsIgnoreCase(link.attr("rel")); // remove altogether if (noFollow && robots_noFollow_strict) { continue; } // but whole page is if (!noFollow && robotsTags.isNoFollow()) { noFollow = true; } String anchor = link.text(); if (StringUtils.isNotBlank(targetURL)) { // any existing anchors for the same target? List<String> anchors = slinks.get(targetURL); if (anchors == null) { anchors = new LinkedList<>(); slinks.put(targetURL, anchors); } // track the anchors only if no follow is false if (!noFollow && StringUtils.isNotBlank(anchor)) { anchors.add(anchor); } } } } Element body = jsoupDoc.body(); if (body != null) { text = body.text(); } } catch (Throwable e) { String errorMessage = "Exception while parsing " + url + ": " + e; handleException(url, e, metadata, tuple, "content parsing", errorMessage); return; } // store identified charset in md metadata.setValue("parse.Content-Encoding", charset); long duration = System.currentTimeMillis() - start; LOG.info("Parsed {} in {} msec", url, duration); List<Outlink> outlinks = toOutlinks(url, metadata, slinks); ParseResult parse = new ParseResult(); parse.setOutlinks(outlinks); // parse data of the parent URL ParseData parseData = parse.get(url); parseData.setMetadata(metadata); parseData.setText(text); parseData.setContent(content); // apply the parse filters if any try { parseFilters.filter(url, content, fragment, parse); } catch (RuntimeException e) { String errorMessage = "Exception while running parse filters on " + url + ": " + e; handleException(url, e, metadata, tuple, "content filtering", errorMessage); return; } if (emitOutlinks) { for (Outlink outlink : parse.getOutlinks()) { collector.emit(StatusStreamName, tuple, new Values(outlink.getTargetURL(), outlink.getMetadata(), Status.DISCOVERED)); } } for (Map.Entry<String, ParseData> doc : parse) { ParseData parseDoc = doc.getValue(); collector.emit(tuple, new Values(doc.getKey(), parseDoc.getContent(), parseDoc.getMetadata(), parseDoc.getText())); } collector.ack(tuple); eventCounter.scope("tuple_success").incr(); }
37. DescriptorFactoryTest#testComponent()
Project: servicemix4-nmr
File: DescriptorFactoryTest.java
File: DescriptorFactoryTest.java
public void testComponent() throws Exception { Descriptor root = DescriptorFactory.buildDescriptor(getClass().getResource("component.xml")); assertNotNull("Unable to parse descriptor", root); // component stuff ComponentDesc component = root.getComponent(); assertNotNull("component is null", component); assertEquals("getBootstrapClassName", "com.foo.Engine1Bootstrap", component.getBootstrapClassName()); assertEquals("getComponentClassName", "com.foo.Engine1", component.getComponentClassName()); assertEquals("getComponentClassPath", new ClassPath(new String[] { "Engine1.jar" }), component.getComponentClassPath()); assertEquals("getBootstrapClassPath", new ClassPath(new String[] { "Engine2.jar" }), component.getBootstrapClassPath()); assertEquals("getDescription", "foo", component.getDescription()); assertArrayEquals("getSharedLibraries", new SharedLibraryList[] { new SharedLibraryList("slib1") }, component.getSharedLibraries()); Identification identification = component.getIdentification(); assertNotNull("identification is null", identification); assertEquals("getName", "example-engine-1", identification.getName()); assertEquals("getDescription", "An example service engine", identification.getDescription()); InstallationDescriptorExtension descriptorExtension = component.getDescriptorExtension(); assertNotNull("descriptorExtension is null", descriptorExtension); DocumentFragment fragment = descriptorExtension.getDescriptorExtension(); assertNotNull("fragment is null", fragment); }
38. CarteTest#parse()
Project: pentaho-kettle
File: CarteTest.java
File: CarteTest.java
public static Node parse(String content) throws SAXException, IOException { DOMFragmentParser parser = new DOMFragmentParser(); HTMLDocument document = new HTMLDocumentImpl(); DocumentFragment fragment = document.createDocumentFragment(); InputSource is = new InputSource(new StringReader(content)); parser.parse(is, fragment); return fragment; }
39. Issue2290Test#testTransform()
Project: openjdk
File: Issue2290Test.java
File: Issue2290Test.java
@Test public final void testTransform() throws Exception { DocumentFragment outNode = null; DocumentBuilder docBuilder = null; Document outDoc = null; // TransformerImpl transformer = null; StringReader execReaderXML = null; Properties propFormat = null; StringWriter sw = null; try { // template = TransformerFactory.newInstance().newTemplates(new // StreamSource("D:/Work/Apache/TestVar.xsl")); // transformer = (TransformerImpl) template.newTransformer(); Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResourceAsStream("Issue2290.xsl"))); System.out.print("Created Transformer"); execReaderXML = new StringReader("<?xml version=\"1.0\"?> <doc>Stuff</doc>"); docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); outDoc = docBuilder.newDocument(); outNode = outDoc.createDocumentFragment(); System.out.println("Created Fragment"); System.out.println("execute transformer."); // transformer.transform(new StreamSource(execReaderXML),new // DOMResult(outNode)); t.transform(new StreamSource(execReaderXML), new DOMResult(outNode)); System.out.println("Finsished transformer."); sw = new StringWriter(); StreamResult sr = new StreamResult(sw); t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.transform(new DOMSource(outNode), sr); System.out.println(sw.toString()); } catch (Exception e) { Assert.fail(e.toString()); } finally { } }
40. RangeImpl#traverseCommonAncestors()
Project: openjdk
File: RangeImpl.java
File: RangeImpl.java
/** * Visits the nodes selected by this range when we know * a-priori that the start and end containers are not * the same, and we also know that neither the start * nor end container is an ancestor of the other. * This method is invoked by * the generic <code>traverse</code> method. * * @param startAncestor * Given a common ancestor of the start and end containers, * this parameter is the ancestor (or self) of the start * container that is a direct child of the common ancestor. * * @param endAncestor * Given a common ancestor of the start and end containers, * this parameter is the ancestor (or self) of the end * container that is a direct child of the common ancestor. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will produce * a document fragment containing the range's content. * Partially selected nodes are copied, but fully * selected nodes are moved. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but sill * produced cloned content in a document fragment * * <li><code>DELETE_CONTENTS</code> - will delete from * the context tree of the range, all fully selected * nodes. * </ol> * * @return Returns a document fragment containing any * copied or extracted nodes. If the <code>how</code> * parameter was <code>DELETE_CONTENTS</code>, the * return value is null. */ private DocumentFragment traverseCommonAncestors(Node startAncestor, Node endAncestor, int how) { DocumentFragment frag = null; if (how != DELETE_CONTENTS) frag = fDocument.createDocumentFragment(); Node n = traverseLeftBoundary(startAncestor, how); if (frag != null) frag.appendChild(n); Node commonParent = startAncestor.getParentNode(); int startOffset = indexOf(startAncestor, commonParent); int endOffset = indexOf(endAncestor, commonParent); ++startOffset; int cnt = endOffset - startOffset; Node sibling = startAncestor.getNextSibling(); while (cnt > 0) { Node nextSibling = sibling.getNextSibling(); n = traverseFullySelected(sibling, how); if (frag != null) frag.appendChild(n); sibling = nextSibling; --cnt; } n = traverseRightBoundary(endAncestor, how); if (frag != null) frag.appendChild(n); if (how != CLONE_CONTENTS) { setStartAfter(startAncestor); collapse(true); } return frag; }
41. RangeImpl#traverseCommonEndContainer()
Project: openjdk
File: RangeImpl.java
File: RangeImpl.java
/** * Visits the nodes selected by this range when we know * a-priori that the start and end containers are not the * same, but the end container is an ancestor of the * start container. This method is invoked by the generic * <code>traverse</code> method. * * @param startAncestor * The ancestor of the start container that is a direct * child of the end container. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will produce * a document fragment containing the range's content. * Partially selected nodes are copied, but fully * selected nodes are moved. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but sill * produced cloned content in a document fragment * * <li><code>DELETE_CONTENTS</code> - will delete from * the context tree of the range, all fully selected * nodes. * </ol> * * @return Returns a document fragment containing any * copied or extracted nodes. If the <code>how</code> * parameter was <code>DELETE_CONTENTS</code>, the * return value is null. */ private DocumentFragment traverseCommonEndContainer(Node startAncestor, int how) { DocumentFragment frag = null; if (how != DELETE_CONTENTS) frag = fDocument.createDocumentFragment(); Node n = traverseLeftBoundary(startAncestor, how); if (frag != null) frag.appendChild(n); int startIdx = indexOf(startAncestor, fEndContainer); // Because we already traversed it.... ++startIdx; int cnt = fEndOffset - startIdx; n = startAncestor.getNextSibling(); while (cnt > 0) { Node sibling = n.getNextSibling(); Node xferNode = traverseFullySelected(n, how); if (frag != null) frag.appendChild(xferNode); --cnt; n = sibling; } if (how != CLONE_CONTENTS) { setStartAfter(startAncestor); collapse(true); } return frag; }
42. RangeImpl#traverseCommonStartContainer()
Project: openjdk
File: RangeImpl.java
File: RangeImpl.java
/** * Visits the nodes selected by this range when we know * a-priori that the start and end containers are not the * same, but the start container is an ancestor of the * end container. This method is invoked by the generic * <code>traverse</code> method. * * @param endAncestor * The ancestor of the end container that is a direct child * of the start container. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will produce * a document fragment containing the range's content. * Partially selected nodes are copied, but fully * selected nodes are moved. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but sill * produced cloned content in a document fragment * * <li><code>DELETE_CONTENTS</code> - will delete from * the context tree of the range, all fully selected * nodes. * </ol> * * @return Returns a document fragment containing any * copied or extracted nodes. If the <code>how</code> * parameter was <code>DELETE_CONTENTS</code>, the * return value is null. */ private DocumentFragment traverseCommonStartContainer(Node endAncestor, int how) { DocumentFragment frag = null; if (how != DELETE_CONTENTS) frag = fDocument.createDocumentFragment(); Node n = traverseRightBoundary(endAncestor, how); if (frag != null) frag.appendChild(n); int endIdx = indexOf(endAncestor, fStartContainer); int cnt = endIdx - fStartOffset; if (cnt <= 0) { // is partially selected. if (how != CLONE_CONTENTS) { setEndBefore(endAncestor); collapse(false); } return frag; } n = endAncestor.getPreviousSibling(); while (cnt > 0) { Node sibling = n.getPreviousSibling(); Node xferNode = traverseFullySelected(n, how); if (frag != null) frag.insertBefore(xferNode, frag.getFirstChild()); --cnt; n = sibling; } // is partially selected. if (how != CLONE_CONTENTS) { setEndBefore(endAncestor); collapse(false); } return frag; }
43. RangeImpl#traverseSameContainer()
Project: openjdk
File: RangeImpl.java
File: RangeImpl.java
/** * Visits the nodes selected by this range when we know * a-priori that the start and end containers are the same. * This method is invoked by the generic <code>traverse</code> * method. * * @param how Specifies what type of traversal is being * requested (extract, clone, or delete). * Legal values for this argument are: * * <ol> * <li><code>EXTRACT_CONTENTS</code> - will produce * a document fragment containing the range's content. * Partially selected nodes are copied, but fully * selected nodes are moved. * * <li><code>CLONE_CONTENTS</code> - will leave the * context tree of the range undisturbed, but sill * produced cloned content in a document fragment * * <li><code>DELETE_CONTENTS</code> - will delete from * the context tree of the range, all fully selected * nodes. * </ol> * * @return Returns a document fragment containing any * copied or extracted nodes. If the <code>how</code> * parameter was <code>DELETE_CONTENTS</code>, the * return value is null. */ private DocumentFragment traverseSameContainer(int how) { DocumentFragment frag = null; if (how != DELETE_CONTENTS) frag = fDocument.createDocumentFragment(); // If selection is empty, just return the fragment if (fStartOffset == fEndOffset) return frag; // Text node needs special case handling if (fStartContainer.getNodeType() == Node.TEXT_NODE) { // get the substring String s = fStartContainer.getNodeValue(); String sub = s.substring(fStartOffset, fEndOffset); // set the original text node to its new value if (how != CLONE_CONTENTS) { ((TextImpl) fStartContainer).deleteData(fStartOffset, fEndOffset - fStartOffset); // Nothing is partially selected, so collapse to start point collapse(true); } if (how == DELETE_CONTENTS) return null; frag.appendChild(fDocument.createTextNode(sub)); return frag; } // Copy nodes between the start/end offsets. Node n = getSelectedNode(fStartContainer, fStartOffset); int cnt = fEndOffset - fStartOffset; while (cnt > 0) { Node sibling = n.getNextSibling(); Node xferNode = traverseFullySelected(n, how); if (frag != null) frag.appendChild(xferNode); --cnt; n = sibling; } // Nothing is partially selected, so collapse to start point if (how != CLONE_CONTENTS) collapse(true); return frag; }
44. RangeImpl#surroundContents()
Project: openjdk
File: RangeImpl.java
File: RangeImpl.java
public void surroundContents(Node newParent) throws DOMException, RangeException { if (newParent == null) return; int type = newParent.getNodeType(); if (fDocument.errorChecking) { if (fDetach) { throw new DOMException(DOMException.INVALID_STATE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_STATE_ERR", null)); } if (type == Node.ATTRIBUTE_NODE || type == Node.ENTITY_NODE || type == Node.NOTATION_NODE || type == Node.DOCUMENT_TYPE_NODE || type == Node.DOCUMENT_NODE || type == Node.DOCUMENT_FRAGMENT_NODE) { throw new RangeExceptionImpl(RangeException.INVALID_NODE_TYPE_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_NODE_TYPE_ERR", null)); } } Node realStart = fStartContainer; Node realEnd = fEndContainer; if (fStartContainer.getNodeType() == Node.TEXT_NODE) { realStart = fStartContainer.getParentNode(); } if (fEndContainer.getNodeType() == Node.TEXT_NODE) { realEnd = fEndContainer.getParentNode(); } if (realStart != realEnd) { throw new RangeExceptionImpl(RangeException.BAD_BOUNDARYPOINTS_ERR, DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "BAD_BOUNDARYPOINTS_ERR", null)); } DocumentFragment frag = extractContents(); insertNode(newParent); newParent.appendChild(frag); selectNode(newParent); }
45. XMLCipher#decryptElement()
Project: jdk7u-jdk
File: XMLCipher.java
File: XMLCipher.java
/** * Decrypts <code>EncryptedData</code> in a single-part operation. * * @param element the <code>EncryptedData</code> to decrypt. * @return the <code>Node</code> as a result of the decrypt operation. * @throws XMLEncryptionException */ private Document decryptElement(Element element) throws XMLEncryptionException { logger.log(java.util.logging.Level.FINE, "Decrypting element..."); if (_cipherMode != DECRYPT_MODE) logger.log(java.util.logging.Level.SEVERE, "XMLCipher unexpectedly not in DECRYPT_MODE..."); String octets; try { octets = new String(decryptToByteArray(element), "UTF-8"); } catch (UnsupportedEncodingException uee) { throw new XMLEncryptionException("empty", uee); } logger.log(java.util.logging.Level.FINE, "Decrypted octets:\n" + octets); Node sourceParent = element.getParentNode(); DocumentFragment decryptedFragment = _serializer.deserialize(octets, sourceParent); if (sourceParent instanceof Document) { // If this is a content decryption, this may have problems _contextDocument.removeChild(_contextDocument.getDocumentElement()); _contextDocument.appendChild(decryptedFragment); } else { sourceParent.replaceChild(decryptedFragment, element); } return (_contextDocument); }
46. NodeCreateRuleTestCase#testImport()
Project: commons-digester
File: NodeCreateRuleTestCase.java
File: NodeCreateRuleTestCase.java
/** * Tests whether the created fragment can be imported into an existing document. */ @Test public void testImport() throws SAXException, ParserConfigurationException, IOException { Digester digester = newLoader(new AbstractRulesModule() { @Override protected void configure() { forPattern("root").createNode().ofType(NodeType.DOCUMENT_FRAGMENT); } }).newDigester(); DocumentFragment fragment = digester.parse(new StringReader(TEST_XML)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); Node importedFragment = doc.importNode(fragment, true); doc.appendChild(doc.createElement("root")); doc.getFirstChild().appendChild(importedFragment); }
47. NodeCreateRuleTestCase#testNamespaces()
Project: commons-digester
File: NodeCreateRuleTestCase.java
File: NodeCreateRuleTestCase.java
/** * Tests whether namespaces are handled correctly, using the example from the file Test3 XML file. */ @Test public void testNamespaces() throws SAXException, IOException { Digester digester = newLoader(new AbstractRulesModule() { @Override protected void configure() { forPattern("employee").createNode().ofType(NodeType.DOCUMENT_FRAGMENT); } }).setNamespaceAware(true).newDigester(); DocumentFragment fragment = digester.parse(getInputStream("Test3.xml")); assertNotNull(fragment); assertEquals(2, fragment.getChildNodes().getLength()); assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType()); Element address1 = (Element) fragment.getFirstChild(); assertEquals("address", address1.getNodeName()); assertEquals("http://commons.apache.org/digester/Bar", address1.getNamespaceURI()); assertEquals("address", address1.getLocalName()); assertEquals(5, address1.getAttributes().getLength()); assertEquals("home", address1.getAttribute("type")); assertEquals("Home Street", address1.getAttribute("street")); assertEquals("Home City", address1.getAttribute("city")); assertEquals("HS", address1.getAttribute("state")); assertEquals("HmZip", address1.getAttribute("zipCode")); assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType()); Element address2 = (Element) fragment.getLastChild(); assertEquals("address", address2.getNodeName()); assertEquals("http://commons.apache.org/digester/Bar", address2.getNamespaceURI()); assertEquals("address", address2.getLocalName()); assertEquals(5, address2.getAttributes().getLength()); assertEquals("office", address2.getAttribute("type")); assertEquals("Office Street", address2.getAttribute("street")); assertEquals("Office City", address2.getAttribute("city")); assertEquals("OS", address2.getAttribute("state")); assertEquals("OfZip", address2.getAttribute("zipCode")); }
48. NodeCreateRuleTestCase#testAttributes()
Project: commons-digester
File: NodeCreateRuleTestCase.java
File: NodeCreateRuleTestCase.java
/** * Tests whether attributes are correctly imported into the fragment, using the example in the Test1 XML file. */ @Test public void testAttributes() throws SAXException, IOException { Digester digester = newLoader(new AbstractRulesModule() { @Override protected void configure() { forPattern("employee").createNode().ofType(NodeType.DOCUMENT_FRAGMENT); } }).newDigester(); DocumentFragment fragment = digester.parse(getInputStream("Test1.xml")); assertNotNull(fragment); assertEquals(2, fragment.getChildNodes().getLength()); assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType()); Element address1 = (Element) fragment.getFirstChild(); assertEquals("address", address1.getNodeName()); assertEquals(5, address1.getAttributes().getLength()); assertEquals("home", address1.getAttribute("type")); assertEquals("Home Street", address1.getAttribute("street")); assertEquals("Home City", address1.getAttribute("city")); assertEquals("HS", address1.getAttribute("state")); assertEquals("HmZip", address1.getAttribute("zipCode")); assertEquals(Node.ELEMENT_NODE, fragment.getLastChild().getNodeType()); Element address2 = (Element) fragment.getLastChild(); assertEquals("address", address2.getNodeName()); assertEquals(5, address2.getAttributes().getLength()); assertEquals("office", address2.getAttribute("type")); assertEquals("Office Street", address2.getAttribute("street")); assertEquals("Office City", address2.getAttribute("city")); assertEquals("OS", address2.getAttribute("state")); assertEquals("OfZip", address2.getAttribute("zipCode")); }
49. NodeCreateRuleTestCase#testNested()
Project: commons-digester
File: NodeCreateRuleTestCase.java
File: NodeCreateRuleTestCase.java
/** * Tests whether control is returned to digester after fragment construction. */ @Test public void testNested() throws SAXException, IOException { Digester digester = newLoader(new AbstractRulesModule() { @Override protected void configure() { forPattern("root").createObject().ofType(ArrayList.class); forPattern("root/a/b").createNode().ofType(NodeType.DOCUMENT_FRAGMENT).then().setRoot("add"); forPattern("root/b").createObject().ofType(String.class).then().setRoot("add"); } }).newDigester(); List<?> list = digester.parse(getInputStream("Test4.xml")); assertNotNull(list); assertEquals(2, list.size()); assertTrue(list.get(0) instanceof DocumentFragment); DocumentFragment fragment = (DocumentFragment) list.get(0); assertEquals(Node.ELEMENT_NODE, fragment.getFirstChild().getNodeType()); Element a = (Element) fragment.getFirstChild(); assertEquals("a", a.getNodeName()); assertEquals(1, a.getAttributes().getLength()); assertEquals("THREE", a.getAttribute("name")); assertTrue(list.get(1) instanceof String); }
50. NodeCreateRuleTestCase#testDocumentFragment()
Project: commons-digester
File: NodeCreateRuleTestCase.java
File: NodeCreateRuleTestCase.java
/** * Tests simple fragment construction, using the {@link #TEST_XML} XML input data. */ @Test public void testDocumentFragment() throws SAXException, IOException { Digester digester = newLoader(new AbstractRulesModule() { @Override protected void configure() { forPattern("root").createNode().ofType(NodeType.DOCUMENT_FRAGMENT); } }).newDigester(); DocumentFragment fragment = digester.parse(new StringReader(TEST_XML)); assertNotNull(fragment); assertEquals(4, fragment.getChildNodes().getLength()); Node rootBody = fragment.getFirstChild(); assertEquals(Node.TEXT_NODE, rootBody.getNodeType()); assertEquals("ROOT BODY", rootBody.getNodeValue()); Node alpha = fragment.getChildNodes().item(1); assertEquals(Node.ELEMENT_NODE, alpha.getNodeType()); assertEquals("alpha", alpha.getNodeName()); assertNull(((Element) alpha).getLocalName()); assertNull(((Element) alpha).getNamespaceURI()); assertEquals(1, alpha.getChildNodes().getLength()); assertEquals("ALPHA BODY", alpha.getFirstChild().getNodeValue()); Node beta = fragment.getChildNodes().item(2); assertEquals(Node.ELEMENT_NODE, beta.getNodeType()); assertEquals("beta", beta.getNodeName()); assertNull(((Element) beta).getLocalName()); assertNull(((Element) beta).getNamespaceURI()); assertEquals(1, beta.getChildNodes().getLength()); assertEquals("BETA BODY", beta.getFirstChild().getNodeValue()); Node gamma = fragment.getChildNodes().item(3); assertEquals(Node.ELEMENT_NODE, gamma.getNodeType()); assertEquals("gamma", gamma.getNodeName()); assertNull(((Element) gamma).getLocalName()); assertNull(((Element) gamma).getNamespaceURI()); assertEquals(1, gamma.getChildNodes().getLength()); assertEquals("GAMMA BODY", gamma.getFirstChild().getNodeValue()); }
51. DOMUtil#getDocumentFragment()
Project: cocoon
File: DOMUtil.java
File: DOMUtil.java
/** * Get a document fragment from a <code>Reader</code>. The reader must * provide valid XML, but it is allowed that the XML has more than one root * node. This xml is parsed by the specified parser instance and a DOM * DocumentFragment is created. */ public static DocumentFragment getDocumentFragment(SAXParser parser, Reader stream) throws ProcessingException { DocumentFragment frag = null; Writer writer; Reader reader; boolean removeRoot = true; try { // create a writer, // write the root element, then the input from the // reader writer = new StringWriter(); writer.write(XML_ROOT_DEFINITION); char[] cbuf = new char[16384]; int len; do { len = stream.read(cbuf, 0, 16384); if (len != -1) { writer.write(cbuf, 0, len); } } while (len != -1); writer.write("</root>"); // now test if xml input start with <?xml String xml = writer.toString(); String searchString = XML_ROOT_DEFINITION + "<?xml "; if (xml.startsWith(searchString)) { // now remove the surrounding root element xml = xml.substring(XML_ROOT_DEFINITION.length(), xml.length() - 7); removeRoot = false; } reader = new StringReader(xml); InputSource input = new InputSource(reader); DOMBuilder builder = new DOMBuilder(); builder.startDocument(); builder.startElement("", "root", "root", XMLUtils.EMPTY_ATTRIBUTES); IncludeXMLConsumer filter = new IncludeXMLConsumer(builder, builder); parser.parse(input, filter); builder.endElement("", "root", "root"); builder.endDocument(); // Create Document Fragment, remove <root> final Document doc = builder.getDocument(); frag = doc.createDocumentFragment(); final Node root = doc.getDocumentElement().getFirstChild(); root.normalize(); if (removeRoot == false) { root.getParentNode().removeChild(root); frag.appendChild(root); } else { Node child; while (root.hasChildNodes()) { child = root.getFirstChild(); root.removeChild(child); frag.appendChild(child); } } } catch (SAXException sax) { throw new ProcessingException("SAXException: " + sax, sax); } catch (IOException ioe) { throw new ProcessingException("IOException: " + ioe, ioe); } return frag; }
52. AbstractSAXTransformer#endRecording()
Project: cocoon
File: AbstractSAXTransformer.java
File: AbstractSAXTransformer.java
/** * Stop DOM DocumentFragment recording. * This method returns the resulting DocumentFragment, normalized. */ public DocumentFragment endRecording() throws SAXException { sendEndPrefixMapping(); DOMBuilder builder = (DOMBuilder) removeRecorder(); builder.endElement("", "cocoon", "cocoon"); builder.endDocument(); // Create Document Fragment final Document doc = builder.getDocument(); final DocumentFragment fragment = doc.createDocumentFragment(); final Node root = doc.getDocumentElement(); // Remove empty text nodes and collapse neighbouring text nodes root.normalize(); // Move all nodes into the fragment boolean space = true; while (root.hasChildNodes()) { Node child = root.getFirstChild(); root.removeChild(child); // FIXME: Why leading spaces are trimmed at all? Why not trailing spaces? if (space && child.getNodeType() == Node.TEXT_NODE && child.getNodeValue().trim().length() == 0) { continue; } space = false; fragment.appendChild(child); } if (getLogger().isDebugEnabled()) { Object serializedXML = null; try { serializedXML = fragment == null ? "null" : XMLUtils.serializeNode(fragment, XMLUtils.createPropertiesForXML(false)); } catch (ProcessingException ignore) { serializedXML = fragment; } getLogger().debug("End recording. Fragment=" + serializedXML); } return fragment; }
53. SimpleSessionContext#getXML()
Project: cocoon
File: SimpleSessionContext.java
File: SimpleSessionContext.java
public synchronized DocumentFragment getXML(String path) throws ProcessingException { DocumentFragment result = null; NodeList list; path = this.createPath(path); String[] pathComponents = DOMUtil.buildPathArray(path); if (pathComponents == null) { list = this.xpathProcessor.selectNodeList(this.data, path); } else { list = DOMUtil.getNodeListFromPath(data, pathComponents); } if (list != null && list.getLength() > 0) { Document doc = DOMUtil.createDocument(); result = doc.createDocumentFragment(); for (int i = 0; i < list.getLength(); i++) { // the found node is either an attribute or an element if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { // if it is an attribute simple create a new text node with the value of the attribute result.appendChild(doc.createTextNode(list.item(i).getNodeValue())); } else { // now we have an element // copy all children of this element in the resulting tree NodeList childs = list.item(i).getChildNodes(); if (childs != null) { for (int m = 0; m < childs.getLength(); m++) { result.appendChild(doc.importNode(childs.item(m), true)); } } } } } return result; }
54. RequestSessionContext#getXML()
Project: cocoon
File: RequestSessionContext.java
File: RequestSessionContext.java
/** * Get the XML from the request object */ public DocumentFragment getXML(String path) throws ProcessingException { if (path == null || path.charAt(0) != '/') { throw new ProcessingException("Not a valid XPath: " + path); } path = this.createPath(path); DocumentFragment result = null; NodeList list; try { list = DOMUtil.selectNodeList(this.contextData, path, this.xpathProcessor); } catch (javax.xml.transform.TransformerException localException) { throw new ProcessingException("Exception: " + localException, localException); } if (list != null && list.getLength() > 0) { result = DOMUtil.getOwnerDocument(contextData).createDocumentFragment(); for (int i = 0; i < list.getLength(); i++) { // the found node is either an attribute or an element if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { // if it is an attribute simple create a new text node with the value of the attribute result.appendChild(DOMUtil.getOwnerDocument(contextData).createTextNode(list.item(i).getNodeValue())); } else { // now we have an element // copy all children of this element in the resulting tree NodeList childs = list.item(i).getChildNodes(); if (childs != null) { for (int m = 0; m < childs.getLength(); m++) { result.appendChild(DOMUtil.getOwnerDocument(contextData).importNode(childs.item(m), true)); } } } } } return result; }
55. DefaultSessionManager#getContextFragment()
Project: cocoon
File: DefaultSessionManager.java
File: DefaultSessionManager.java
/** * Get information from the context. * A document fragment containg the xml data stored in the session context * with the given name is returned. If the information is not available, * <CODE>null</CODE> is returned. * @param contextName The name of the public context. * @param path XPath expression specifying which data to get. * @return A DocumentFragment containing the data or <CODE>null</CODE> */ public DocumentFragment getContextFragment(String contextName, String path) throws ProcessingException { // synchronized via context if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("BEGIN getContextFragment name=" + contextName + ", path=" + path); } // test arguments if (contextName == null) { throw new ProcessingException("SessionManager.getContextFragment: Name is required"); } if (path == null) { throw new ProcessingException("SessionManager.getContextFragment: Path is required"); } SessionContext context = this.contextManager.getContext(contextName); if (context == null) { throw new ProcessingException("SessionManager.getContextFragment: Context '" + contextName + "' not found."); } DocumentFragment frag; frag = context.getXML(path); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END getContextFragment documentFragment=" + (frag == null ? "null" : XMLUtils.serializeNode(frag, XMLUtils.createPropertiesForXML(false)))); } return frag; }
56. DefaultFormManager#registerInputField()
Project: cocoon
File: DefaultFormManager.java
File: DefaultFormManager.java
/** * @see FormManager#registerInputField(String, String, String, String) */ public DocumentFragment registerInputField(String contextName, String path, String name, String formName) throws ProcessingException { // synchronized if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("BEGIN registerInputField context=" + contextName + ", path=" + path + ", name=" + name + ", formName=" + formName); } // test arguments if (contextName == null) { throw new ProcessingException("SessionManager.registerInputField: Context Name is required"); } if (path == null) { throw new ProcessingException("SessionManager.registerInputField: Path is required"); } if (name == null) { throw new ProcessingException("SessionManager.registerInputField: Name is required"); } if (formName == null) { throw new ProcessingException("SessionManager.registerInputField: Form is required"); } DocumentFragment value = null; SessionContext context = this.getContext(contextName); if (context == null) { throw new ProcessingException("SessionManager.registerInputField: Context not found " + contextName); } final Request request = ContextHelper.getRequest(this.context); Session session = request.getSession(false); if (session == null) { throw new ProcessingException("SessionManager.registerInputField: Session is required for context " + contextName); } synchronized (session) { Map inputFields = (Map) session.getAttribute(ATTRIBUTE_INPUTXML_STORAGE); if (inputFields == null) { inputFields = new HashMap(10); session.setAttribute(ATTRIBUTE_INPUTXML_STORAGE, inputFields); } inputFields.put(name, new Object[] { context, path, formName }); value = context.getXML(path); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END registerInputField value=" + value); } return value; }
57. XSPSessionFwHelper#getXMLAsString()
Project: cocoon
File: XSPSessionFwHelper.java
File: XSPSessionFwHelper.java
/** GetXML Fragment from the given session context and path * * * @param cm The ComponentManager * @param context The Session context tha define where to search * @param path The parameter path **/ public static String getXMLAsString(ComponentManager cm, String context, String path) throws ProcessingException { DocumentFragment df = getXML(cm, context, path); return df != null ? df.getFirstChild().getNodeValue() : ""; }
58. SessionContextImpl#getXML()
Project: cocoon
File: SessionContextImpl.java
File: SessionContextImpl.java
/** * Get the xml fragment */ public synchronized DocumentFragment getXML(String path) throws ProcessingException { DocumentFragment result = null; if (path.startsWith("/")) path = path.substring(1); NodeList list = null; if (path == null || path.length() == 0) { Document doc = DOMUtil.createDocument(); result = doc.createDocumentFragment(); this.getLayoutDOM(); if (this.layoutDOM != null) { result.appendChild(doc.importNode(this.layoutDOM.getDocumentElement(), true)); } if (this.configurationDOM != null) { result.appendChild(doc.importNode(this.configurationDOM.getDocumentElement(), true)); } if (this.statusProfile != null) { if (this.copletID != null && this.copletNumber != null) { String statusPath = "customization/coplet[@id='" + copletID + "' and @number='" + copletNumber + "']"; try { Node node = DOMUtil.getSingleNode(this.statusProfile, statusPath, this.xpathProcessor); if (node != null) { Element copletData = doc.createElementNS(null, "coplet-data"); NodeList childs = node.getChildNodes(); if (childs != null) { for (int l = 0; l < childs.getLength(); l++) { copletData.appendChild(doc.importNode(childs.item(l), true)); } } result.appendChild(copletData); } } catch (javax.xml.transform.TransformerException localException) { throw new ProcessingException("TransformerException: " + localException, localException); } } } } if (path.equals("layout") || path.startsWith("layout/")) { try { this.getLayoutDOM(); if (this.layoutDOM != null) list = DOMUtil.selectNodeList(this.layoutDOM, path, this.xpathProcessor); } catch (javax.xml.transform.TransformerException localException) { throw new ProcessingException("TransformerException: " + localException, localException); } } if (path.equals("configuration") || path.startsWith("configuration/")) { try { if (this.configurationDOM != null) list = DOMUtil.selectNodeList(this.configurationDOM, path, this.xpathProcessor); } catch (javax.xml.transform.TransformerException localException) { throw new ProcessingException("TransformerException: " + localException, localException); } } if (path.startsWith("coplet-data/") || path.equals("coplet-data")) { if (this.statusProfile != null) { if (this.copletID != null && this.copletNumber != null) { String statusPath = "customization/coplet[@id='" + copletID + "' and @number='" + copletNumber + "']"; if (path.startsWith("coplet-data/")) { statusPath = statusPath + path.substring(11); } try { list = DOMUtil.selectNodeList(this.statusProfile, statusPath, this.xpathProcessor); } catch (javax.xml.transform.TransformerException localException) { throw new ProcessingException("TransformerException: " + localException, localException); } } } } if (list != null && list.getLength() > 0) { Document doc = DOMUtil.createDocument(); result = doc.createDocumentFragment(); for (int i = 0; i < list.getLength(); i++) { // the found node is either an attribute or an element if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) { // if it is an attribute simple create a new text node with the value of the attribute result.appendChild(doc.createTextNode(list.item(i).getNodeValue())); } else { // now we have an element // copy all children of this element in the resulting tree NodeList childs = list.item(i).getChildNodes(); if (childs != null) { for (int m = 0; m < childs.getLength(); m++) { result.appendChild(doc.importNode(childs.item(m), true)); } } } } } return result; }
59. PortalManagerImpl#buildUserProfile()
Project: cocoon
File: PortalManagerImpl.java
File: PortalManagerImpl.java
/** * Build the user profile */ private void buildUserProfile(Element profileRoot, Map config, String role, String id, boolean adminProfile) throws ProcessingException { // calling method is synced DocumentFragment userFragment; RequestState reqstate = this.getRequestState(); SourceParameters pars; pars = new SourceParameters(); pars.setSingleParameterValue("ID", id); pars.setSingleParameterValue("role", role); pars.setSingleParameterValue("application", reqstate.getApplicationName()); pars.setSingleParameterValue("handler", reqstate.getHandlerName()); pars.setSingleParameterValue("profile", "user-delta"); String res = (String) config.get(PortalConstants.CONF_USERDELTA_LOADRESOURCE); if (res != null) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading user profile"); } userFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("user profile loaded"); } this.importProfileDelta(profileRoot, userFragment, "user-delta", "layout-delta"); this.importProfileDelta(profileRoot, userFragment, "user-delta", "coplets-delta"); this.addProfilePart(profileRoot, userFragment, "user-delta", "portal-profile"); this.importProfileDelta(profileRoot, userFragment, "user-delta", "personal-delta"); } // types res = (String) config.get(PortalConstants.CONF_USERDELTA_TYPERESOURCE); if (!adminProfile && res != null) { pars.setSingleParameterValue("profile", "user-type-delta"); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading user type profile"); } userFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("user type profile loaded"); } this.addProfilePart(profileRoot, userFragment, "user-delta", "type-profile"); } }
60. PortalManagerImpl#buildRoleProfile()
Project: cocoon
File: PortalManagerImpl.java
File: PortalManagerImpl.java
/** * Build the role profile */ private void buildRoleProfile(Element profileRoot, Map config, String role, boolean adminProfile) throws ProcessingException { // calling method is synced DocumentFragment roleFragment; RequestState reqstate = this.getRequestState(); SourceParameters pars; pars = new SourceParameters(); pars.setSingleParameterValue("role", role); pars.setSingleParameterValue("application", reqstate.getApplicationName()); pars.setSingleParameterValue("handler", reqstate.getHandlerName()); pars.setSingleParameterValue("profile", "role-delta"); String res = (String) config.get(PortalConstants.CONF_ROLEDELTA_LOADRESOURCE); if (res != null) { if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading role profile"); } roleFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("role profile loaded"); } this.importProfileDelta(profileRoot, roleFragment, "role-delta", "layout-delta"); this.importProfileDelta(profileRoot, roleFragment, "role-delta", "coplets-delta"); this.addProfilePart(profileRoot, roleFragment, "role-delta", "portal-profile"); this.importProfileDelta(profileRoot, roleFragment, "role-delta", "personal-delta"); } // types res = (String) config.get(PortalConstants.CONF_ROLEDELTA_TYPERESOURCE); if (!adminProfile && res != null) { pars.setSingleParameterValue("profile", "role-type-delta"); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading role type profile"); } roleFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("role type profile loaded"); } this.addProfilePart(profileRoot, roleFragment, "role-delta", "type-profile"); } }
61. PortalManagerImpl#buildGlobalProfile()
Project: cocoon
File: PortalManagerImpl.java
File: PortalManagerImpl.java
/** * Build the global profile. */ private void buildGlobalProfile(Element profileRoot, Map config, boolean adminProfile) throws ProcessingException { // calling method is synced if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("BEGIN buildGlobalProfile profileRoot=" + profileRoot + ", config=" + config + ", adminProfile=" + adminProfile); } DocumentFragment globalFragment; String res = (String) config.get(PortalConstants.CONF_GLOBALDELTA_LOADRESOURCE); if (res == null) { throw new ProcessingException("No configuration for portal-role delta profile found."); } SourceParameters pars = new SourceParameters(); RequestState reqstate = this.getRequestState(); pars.setSingleParameterValue("application", reqstate.getApplicationName()); pars.setSingleParameterValue("handler", reqstate.getHandlerName()); pars.setSingleParameterValue("profile", "global-delta"); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading global profile"); } globalFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("global profile loaded"); } this.importProfileDelta(profileRoot, globalFragment, "global-delta", "layout-delta"); this.importProfileDelta(profileRoot, globalFragment, "global-delta", "coplets-delta"); this.addProfilePart(profileRoot, globalFragment, "global-delta", "portal-profile"); this.addProfilePart(profileRoot, globalFragment, "global-delta", "personal-profile"); // types res = (String) config.get(PortalConstants.CONF_GLOBALDELTA_TYPERESOURCE); if (!adminProfile && res != null) { pars.setSingleParameterValue("profile", "global-type-delta"); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("loading global type profile"); } globalFragment = SourceUtil.readDOM(res, null, pars, this.resolver); if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("global type profile loaded"); } this.addProfilePart(profileRoot, globalFragment, "global-delta", "type-profile"); } if (this.getLogger().isDebugEnabled()) { this.getLogger().debug("END buildGlobalProfile"); } }
62. ContextGrabber#grab()
Project: cocoon
File: ContextGrabber.java
File: ContextGrabber.java
/** * Grabbing the context of the current user * * @param context the instantiated class AuthenticationContext * @return Object of context information */ public UserBean grab(AuthenticationContext context) { UserBean ub = new UserBean(); DocumentFragment df = null; try { df = context.getXML("/authentication/"); } catch (Exception e) { } grabAuthContext(df.getFirstChild(), ub); return ub; }
63. AuthenticationContext#addParametersFromAuthenticationXML()
Project: cocoon
File: AuthenticationContext.java
File: AuthenticationContext.java
/** * Convert the authentication XML of a handler to parameters. * The XML is flat and consists of elements which all have exactly one text node: * <parone>value_one<parone> * <partwo>value_two<partwo> * A parameter can occur more than once with different values. */ private void addParametersFromAuthenticationXML(String path, SourceParameters parameters) throws ProcessingException { final DocumentFragment fragment = this.authContext.getXML("/authentication" + path); if (fragment != null) { NodeList childs = fragment.getChildNodes(); if (childs != null) { Node current; for (int i = 0; i < childs.getLength(); i++) { current = childs.item(i); // only element nodes if (current.getNodeType() == Node.ELEMENT_NODE) { current.normalize(); NodeList valueChilds = current.getChildNodes(); String key; StringBuffer valueBuffer; String value; key = current.getNodeName(); valueBuffer = new StringBuffer(); for (int m = 0; m < valueChilds.getLength(); m++) { // attention: current is reused here! current = valueChilds.item(m); if (current.getNodeType() == Node.TEXT_NODE) { // only text nodes if (valueBuffer.length() > 0) valueBuffer.append(' '); valueBuffer.append(current.getNodeValue()); } } value = valueBuffer.toString().trim(); if (key != null && value != null && value.length() > 0) { parameters.setParameter(key, value); } } } } } }
64. AuthenticationContext#getXML()
Project: cocoon
File: AuthenticationContext.java
File: AuthenticationContext.java
/* (non-Javadoc) * @see org.apache.cocoon.webapps.session.context.SessionContext#getXML(java.lang.String) */ public DocumentFragment getXML(String path) throws ProcessingException { if (path == null) { throw new ProcessingException("getXML: Path is required"); } if (!path.startsWith("/")) path = '/' + path; final String applicationName = this.getState().getApplicationName(); DocumentFragment frag = null; if (path.equals("/")) { // get all: first authentication then application frag = this.authContext.getXML("/authentication"); if (frag != null) { // now add root node authentication Node root = frag.getOwnerDocument().createElementNS(null, "authentication"); Node child; while (frag.hasChildNodes()) { child = frag.getFirstChild(); frag.removeChild(child); root.appendChild(child); } frag.appendChild(root); } if (applicationName != null) { // join DocumentFragment appFrag = this.authContext.getXML("/applications/" + applicationName); if (appFrag != null) { // now add root node application Node root = appFrag.getOwnerDocument().createElementNS(null, "application"); Node child; while (appFrag.hasChildNodes()) { child = appFrag.getFirstChild(); appFrag.removeChild(child); root.appendChild(child); } appFrag.appendChild(root); if (frag == null) { frag = appFrag; } else { while (appFrag.hasChildNodes()) { child = appFrag.getFirstChild(); appFrag.removeChild(child); child = frag.getOwnerDocument().importNode(child, true); frag.appendChild(child); } } } } } else if (path.startsWith("/authentication")) { frag = this.authContext.getXML(path); } else if (path.equals("/application") || path.startsWith("/application/")) { if (applicationName != null) { String appPath; if (path.equals("/application")) { appPath = "/"; } else { appPath = path.substring("/application".length()); } frag = this.authContext.getXML("/applications/" + applicationName + appPath); } } else { frag = this.authContext.getXML(path); } return frag; }
65. DOMTreeBuilder#getDocumentFragment()
Project: caja
File: DOMTreeBuilder.java
File: DOMTreeBuilder.java
/** * Return the document fragment. * * @return the document fragment */ DocumentFragment getDocumentFragment() { DocumentFragment rv = document.createDocumentFragment(); Node rootElt = document.getFirstChild(); while (rootElt.hasChildNodes()) { rv.appendChild(rootElt.getFirstChild()); } document = null; return rv; }
66. NodesTest#assertDocType()
Project: caja
File: NodesTest.java
File: NodesTest.java
private void assertDocType(String expected, String docType) throws ParseException { Document doc = DomParser.makeDocument(DoctypeMaker.parse(docType), null); DocumentFragment html = html(fromString("TEST NODE")); doc.appendChild(doc.adoptNode(html)); MoreAsserts.assertStartsWith(expected, Nodes.render(doc.getDoctype(), html, null)); }
67. NodesTest#assertNoDocType()
Project: caja
File: NodesTest.java
File: NodesTest.java
private void assertNoDocType(String docType) throws ParseException { Document doc = DomParser.makeDocument(DoctypeMaker.parse(docType), null); DocumentFragment html = html(fromString("TEST NODE")); doc.appendChild(doc.adoptNode(html)); assertFalse(Nodes.render(doc.getDoctype(), html, null).matches("[<][!]DOCTYPE")); }
68. NodesTest#testRenderSpeed()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderSpeed() throws Exception { DocumentFragment doc = html(fromResource("amazon.com.html")); // prime the JIT benchmark(100, doc); // Let the JIT kick-in. Thread.sleep(250); int microsPerRun = benchmark(250, doc); // See extractVarZ in "tools/dashboard/dashboard.pl". // This should be named usPerRun, but there is already history with the // broken name. System.out.println(" VarZ:" + getClass().getName() + ".msPerRun=" + microsPerRun); }
69. NodesTest#testRenderWithBrokenNekoDom()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderWithBrokenNekoDom() throws Exception { DocumentFragment html = html(fromString("<a href='foo.html'>bar</a>")); assertEquals("<html><head></head><body><a href=\"foo.html\">bar</a></body></html>", Nodes.render(new NullLocalNameMembrane().wrap((Element) getOnlyChild(html), Element.class))); }
70. NodesTest#testRenderWithMaskedInputNamespace2()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderWithMaskedInputNamespace2() throws Exception { DocumentFragment fragment = xmlFragment(fromString("<svg:foo><svg:bar xmlns:svg='http://foo/'/></svg:foo>")); assertEquals("<svg:foo><_ns1:bar xmlns:_ns1=\"http://foo/\"></_ns1:bar></svg:foo>", Nodes.render(fragment)); }
71. NodesTest#testRenderWithMaskedInputNamespace1()
Project: caja
File: NodesTest.java
File: NodesTest.java
public final void testRenderWithMaskedInputNamespace1() throws Exception { DocumentFragment fragment = xmlFragment(fromString("<svg:foo><svg:bar xmlns:svg='" + Namespaces.HTML_NAMESPACE_URI + "'/></svg:foo>")); assertEquals("<svg:foo><bar></bar></svg:foo>", Nodes.render(fragment)); }
72. DomParserTest#testCommentsHidingCdataEnd()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testCommentsHidingCdataEnd() throws Exception { DocumentFragment t = xmlFragment(fromString("<xmp> <!-- </xmp> --> </xmp>")); assertEquals("<xmp> </xmp>", Nodes.render(t, MarkupRenderMode.XML)); assertEquals("<xmp> </xmp>", Nodes.render(t, MarkupRenderMode.HTML)); }
73. DomParserTest#testRender()
Project: caja
File: DomParserTest.java
File: DomParserTest.java
public final void testRender() throws Exception { DocumentFragment t = xmlFragment(fromString("" + "<script><![CDATA[ foo() < bar() ]]></script>\n" + "<p />\n" + "<![CDATA[ 1 < 2 && 3 > 4 ]]>\n" + "<xmp>1 < 2</xmp>\n" + "<script> foo() < bar() </script>")); // Rendered as XML assertEquals("" + "<script> foo() < bar() </script>\n" + "<p></p>\n" + " 1 < 2 && 3 > 4 \n" + "<xmp>1 < 2</xmp>\n" + "<script> foo() < bar() </script>", Nodes.render(t, MarkupRenderMode.XML)); // Rendered as HTML assertEquals("" + "<script> foo() < bar() </script>\n" + "<p></p>\n" + " 1 < 2 && 3 > 4 \n" + "<xmp>1 < 2</xmp>\n" + "<script> foo() < bar() </script>", Nodes.render(t, MarkupRenderMode.HTML)); }
74. XmlElementStack#finish()
Project: caja
File: XmlElementStack.java
File: XmlElementStack.java
/** {@inheritDoc} */ public void finish(FilePosition endOfDocument) throws IllegalDocumentStateException { stripIgnorableText(); DocumentFragment root = getRootElement(); if (needsDebugData) { FilePosition rootStart = Nodes.getFilePositionFor(root); if (rootStart == null || InputSource.UNKNOWN.equals(rootStart.source())) { if (root.getFirstChild() == null) { rootStart = endOfDocument; } else { rootStart = Nodes.getFilePositionFor(root.getFirstChild()); } } if (rootStart.startCharInFile() <= endOfDocument.startCharInFile()) { Nodes.setFilePositionFor(root, FilePosition.span(rootStart, endOfDocument)); } } int nOpen = getNOpenElements(); if (nOpen != 1) { Element openEl = getElement(nOpen - 1); throw new IllegalDocumentStateException(new Message(DomParserMessageType.MISSING_END, endOfDocument, MessagePart.Factory.valueOf(openEl.getTagName()), Nodes.getFilePositionFor(openEl))); } }
75. Html5ElementStack#getRootElement()
Project: caja
File: Html5ElementStack.java
File: Html5ElementStack.java
/** {@inheritDoc} */ public DocumentFragment getRootElement() { // libHtmlParser always produces a document with html, head, and body tags // which we usually don't want, so unroll it. // If we can't throw away the head element, and the body header, then we // return the entire document. Otherwise, we return a document fragment // consisting of the contents of the body. Element root = builder.getRootElement(); DocumentFragment result = doc.createDocumentFragment(); if (needsDebugData) { Nodes.setFilePositionFor(result, builder.getFragmentBounds()); } final Node first = root.getFirstChild(); if (!isFragment || topLevelHtmlFromInput) { result.appendChild(root); return result; } // If disposing of the html, body, or head elements would lose info don't // do it, so look for attributes. boolean tagsBesidesHeadBodyFrameset = false; boolean topLevelTagsWithAttributes = hasSpecifiedAttributes(root); for (Node child = first; child != null; child = child.getNextSibling()) { if (child instanceof Element) { Element el = (Element) child; String tagName = el.getTagName(); if (!("head".equals(tagName) || "body".equals(tagName) || "frameset".equals(tagName))) { tagsBesidesHeadBodyFrameset = true; break; } if (!topLevelTagsWithAttributes && hasSpecifiedAttributes(el) && // with attributes. !"frameset".equals(tagName)) { topLevelTagsWithAttributes = true; } } } // <html><frameset>...</frameset></html> if (tagsBesidesHeadBodyFrameset || topLevelTagsWithAttributes) { // Merging the body and head would lose info. result.appendChild(root); return result; } // Merge the body and head into a fragment. // Convert // <html> // <head> // <link rel=stylesheet ...> // </head> // <body> // <p>Hello World</p. // </body> // </html> // to // #fragment // <link rel=stylesheet ...> // <p>Hello World</p. Node pending = null; for (Node child = first; child != null; child = child.getNextSibling()) { if (child instanceof Element) { String tagName = ((Element) child).getTagName(); if ("head".equals(tagName) || "body".equals(tagName)) { // Shallow descent for (Node grandchild = child.getFirstChild(); grandchild != null; grandchild = grandchild.getNextSibling()) { pending = appendNormalized(pending, grandchild, result); } } else { // reached for framesets pending = child; } } else { pending = appendNormalized(pending, child, result); } } if (pending != null) { result.appendChild(pending); } return result; }
76. DomParser#parseDocument()
Project: caja
File: DomParser.java
File: DomParser.java
/** Parse a document returning the document element. */ private Element parseDocument(String features) throws ParseException { Function<DOMImplementation, DocumentType> doctypeMaker = findDoctype(); Document doc = makeDocument(doctypeMaker, features, domImpl); OpenElementStack elementStack = makeElementStack(doc, mq); // Make sure the elementStack is empty. elementStack.open(false); skipTopLevelDocIgnorables(true); do { parseDom(elementStack); skipTopLevelDocIgnorables(false); } while (!tokens.isEmpty()); FilePosition endPos = checkEnd(elementStack); DocumentFragment root = elementStack.getRootElement(); Node firstChild = root.getFirstChild(); if (firstChild == null || firstChild.getNodeType() != Node.ELEMENT_NODE) { throw new ParseException(new Message(DomParserMessageType.MISSING_DOCUMENT_ELEMENT, endPos)); } // Check that there isn't any extraneous content after the root element. for (Node child = firstChild.getNextSibling(); child != null; child = child.getNextSibling()) { switch(child.getNodeType()) { case Node.COMMENT_NODE: case Node.DOCUMENT_TYPE_NODE: continue; case Node.TEXT_NODE: if ("".equals(child.getNodeValue().trim())) { continue; } break; default: break; } throw new ParseException(new Message(DomParserMessageType.MISPLACED_CONTENT, Nodes.getFilePositionFor(child))); } Nodes.setFilePositionFor(doc, Nodes.getFilePositionFor(root)); doc.appendChild(firstChild); if (elementStack.needsNamespaceFixup()) { firstChild = fixup(firstChild, ns); } return (Element) firstChild; }
77. TikaParser#getParse()
Project: anthelion
File: TikaParser.java
File: TikaParser.java
public ParseResult getParse(Content content) { String mimeType = content.getContentType(); URL base; try { base = new URL(content.getBaseUrl()); } catch (MalformedURLException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } // get the right parser using the mime type as a clue Parser parser = tikaConfig.getParser(MediaType.parse(mimeType)); byte[] raw = content.getContent(); if (parser == null) { String message = "Can't retrieve Tika parser for mime-type " + mimeType; LOG.error(message); return new ParseStatus(ParseStatus.FAILED, message).getEmptyParseResult(content.getUrl(), getConf()); } LOG.debug("Using Tika parser " + parser.getClass().getName() + " for mime-type " + mimeType); Metadata tikamd = new Metadata(); HTMLDocumentImpl doc = new HTMLDocumentImpl(); doc.setErrorChecking(false); DocumentFragment root = doc.createDocumentFragment(); DOMBuilder domhandler = new DOMBuilder(doc, root); ParseContext context = new ParseContext(); try { parser.parse(new ByteArrayInputStream(raw), domhandler, tikamd, context); } catch (Exception e) { LOG.error("Error parsing " + content.getUrl(), e); return new ParseStatus(ParseStatus.FAILED, e.getMessage()).getEmptyParseResult(content.getUrl(), getConf()); } HTMLMetaTags metaTags = new HTMLMetaTags(); String text = ""; String title = ""; Outlink[] outlinks = new Outlink[0]; org.apache.nutch.metadata.Metadata nutchMetadata = new org.apache.nutch.metadata.Metadata(); // we have converted the sax events generated by Tika into a DOM object // so we can now use the usual HTML resources from Nutch // get meta directives HTMLMetaProcessor.getMetaTags(metaTags, root, base); if (LOG.isTraceEnabled()) { LOG.trace("Meta tags for " + base + ": " + metaTags.toString()); } // check meta directives if (// okay to index !metaTags.getNoIndex()) { StringBuffer sb = new StringBuffer(); if (LOG.isTraceEnabled()) { LOG.trace("Getting text..."); } // extract text utils.getText(sb, root); text = sb.toString(); sb.setLength(0); if (LOG.isTraceEnabled()) { LOG.trace("Getting title..."); } // extract title utils.getTitle(sb, root); title = sb.toString().trim(); } if (// okay to follow links !metaTags.getNoFollow()) { // extract outlinks ArrayList<Outlink> l = new ArrayList<Outlink>(); URL baseTag = utils.getBase(root); if (LOG.isTraceEnabled()) { LOG.trace("Getting links..."); } utils.getOutlinks(baseTag != null ? baseTag : base, l, root); outlinks = l.toArray(new Outlink[l.size()]); if (LOG.isTraceEnabled()) { LOG.trace("found " + outlinks.length + " outlinks in " + content.getUrl()); } } // populate Nutch metadata with Tika metadata String[] TikaMDNames = tikamd.names(); for (String tikaMDName : TikaMDNames) { if (tikaMDName.equalsIgnoreCase(Metadata.TITLE)) continue; // TODO what if multivalued? nutchMetadata.add(tikaMDName, tikamd.get(tikaMDName)); } if (outlinks.length == 0) { outlinks = OutlinkExtractor.getOutlinks(text, getConf()); } ParseStatus status = new ParseStatus(ParseStatus.SUCCESS); if (metaTags.getRefresh()) { status.setMinorCode(ParseStatus.SUCCESS_REDIRECT); status.setArgs(new String[] { metaTags.getRefreshHref().toString(), Integer.toString(metaTags.getRefreshTime()) }); } ParseData parseData = new ParseData(status, title, outlinks, content.getMetadata(), nutchMetadata); ParseResult parseResult = ParseResult.createParseResult(content.getUrl(), new ParseImpl(text, parseData)); // run filters on parse ParseResult filteredParse = this.htmlParseFilters.filter(content, parseResult, metaTags, root); if (// not okay to cache metaTags.getNoCache()) { for (Map.Entry<org.apache.hadoop.io.Text, Parse> entry : filteredParse) entry.getValue().getData().getParseMeta().set(Nutch.CACHING_FORBIDDEN_KEY, cachingPolicy); } return filteredParse; }
78. WdcParser#getParse()
Project: anthelion
File: WdcParser.java
File: WdcParser.java
/** * Checks for semantic data in the web page. If there is, the triples will * be stored in the content field. Also, it adds a containsSem boolean field * to the metadata. * */ public ParseResult getParse(Content content) { LOG.info("-------->>>>> WE ARE IN THE PARSER-------------------"); // Generate source document byte[] byteContent = content.getContent(); String contentType = content.getContentType(); String url = content.getUrl(); ByteArrayDocumentSource source = new ByteArrayDocumentSource(byteContent, url, contentType); StringBuffer writer = new StringBuffer(); // gets outlink. we inject here code of parse-html HTMLMetaTags metaTags = new HTMLMetaTags(); URL base; try { base = new URL(content.getBaseUrl()); } catch (MalformedURLException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } String text = ""; String title = ""; boolean containsSem = false; Outlink[] outlinks = new Outlink[0]; Metadata metadata = new Metadata(); // parse the content DocumentFragment root; try { byte[] contentInOctets = content.getContent(); InputSource input = new InputSource(new ByteArrayInputStream(contentInOctets)); EncodingDetector detector = new EncodingDetector(conf); detector.autoDetectClues(content, true); detector.addClue(sniffCharacterEncoding(contentInOctets), "sniffed"); String encoding = detector.guessEncoding(content, defaultCharEncoding); metadata.set(Metadata.ORIGINAL_CHAR_ENCODING, encoding); metadata.set(Metadata.CHAR_ENCODING_FOR_CONVERSION, encoding); input.setEncoding(encoding); if (LOG.isTraceEnabled()) { LOG.trace("Parsing..."); } root = parse(input); } catch (IOException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } catch (DOMException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } catch (SAXException e) { return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } catch (Exception e) { LOG.error("Error: ", e); return new ParseStatus(e).getEmptyParseResult(content.getUrl(), getConf()); } try { FilterableTripleHandler triple = new FilterableTripleHandler(writer, evilNamespaces, notSoEvilNamespaces); // extract any23.extract(any23Params, source, triple); } catch (Exception e) { e.printStackTrace(); } // get meta directives HTMLMetaProcessor.getMetaTags(metaTags, root, base); if (LOG.isTraceEnabled()) { LOG.trace("Meta tags for " + base + ": " + metaTags.toString()); } // check meta directives if (// okay to index !metaTags.getNoIndex()) { StringBuffer sb = new StringBuffer(); if (LOG.isTraceEnabled()) { LOG.trace("Getting text..."); } // utils.getText(sb, root); // extract text text = writer.toString(); if (text != null && countLines(text) > 5) { containsSem = true; } sb.setLength(0); if (LOG.isTraceEnabled()) { LOG.trace("Getting title..."); } // extract title utils.getTitle(sb, root); title = sb.toString().trim(); } writer = null; if (// okay to follow links !metaTags.getNoFollow()) { // extract outlinks ArrayList<Outlink> l = new ArrayList<Outlink>(); URL baseTag = utils.getBase(root); if (LOG.isTraceEnabled()) { LOG.trace("Getting links..."); } utils.getOutlinks(baseTag != null ? baseTag : base, l, root); outlinks = l.toArray(new Outlink[l.size()]); if (LOG.isTraceEnabled()) { LOG.trace("found " + outlinks.length + " outlinks in " + content.getUrl()); } } ParseStatus status = new ParseStatus(ParseStatus.SUCCESS); if (metaTags.getRefresh()) { status.setMinorCode(ParseStatus.SUCCESS_REDIRECT); status.setArgs(new String[] { metaTags.getRefreshHref().toString(), Integer.toString(metaTags.getRefreshTime()) }); } ParseData parseData = new ParseData(status, title, outlinks, content.getMetadata(), metadata); ParseResult parseResult = ParseResult.createParseResult(content.getUrl(), new ParseImpl(text, parseData)); Parse parse = parseResult.get(content.getUrl()); parse.getData().getContentMeta().set(META_CONTAINS_SEM, Boolean.toString(containsSem)); // parseResult, metaTags, root); if (// not okay to cache metaTags.getNoCache()) { for (Map.Entry<org.apache.hadoop.io.Text, Parse> entry : parseResult) entry.getValue().getData().getParseMeta().set(Nutch.CACHING_FORBIDDEN_KEY, cachingPolicy); } return parseResult; }