List of usage examples for org.w3c.dom Document getChildNodes
public NodeList getChildNodes();
NodeList
that contains all children of this node. From source file:org.jboss.dashboard.ui.panel.help.PanelHelpManager.java
protected PanelHelp getPanelHelp(Document doc) { PanelHelpImpl help = new PanelHelpImpl(); NodeList nlist = doc.getChildNodes(); Node rootNode = null;//from w w w . jav a2 s . c o m for (int i = 0; i < nlist.getLength(); i++) { Node item = nlist.item(i); if (PHELP.equals(item.getNodeName())) rootNode = item; } if (rootNode != null) { NodeList rootChildren = rootNode.getChildNodes(); for (int i = 0; i < rootChildren.getLength(); i++) { Node node = rootChildren.item(i); String nodeName = node.getNodeName(); if (PANEL_ID.equals(nodeName)) { NodeList childNodes = node.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node textNode = childNodes.item(j); if ("#text".equals(textNode.getNodeName())) { help.addId(textNode.getNodeValue().trim()); break; } } } else if (USAGE.equals(nodeName)) { Map m = getTextsFromNode(node); for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); Locale locale = (Locale) entry.getKey(); String value = (String) entry.getValue(); help.addUsage(locale, value); } } else if (EDIT_USAGE.equals(nodeName)) { Map m = getTextsFromNode(node); for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); Locale locale = (Locale) entry.getKey(); String value = (String) entry.getValue(); help.addEditUsage(locale, value); } } else if (PANEL_PARAMETER.equals(nodeName)) { String paramName = node.getAttributes().getNamedItem(NAME).getNodeValue(); Map m = getTextsFromNode(node); for (Iterator iterator = m.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); Locale locale = (Locale) entry.getKey(); String value = (String) entry.getValue(); help.addParamDescription(paramName, locale, value); } } else if (ABOUT.equals(nodeName)) { help.setAbout(getAboutFromNode(node)); } } } if (log.isDebugEnabled()) log.debug("Created help " + help); return help; }
From source file:org.jboss.dashboard.workspace.export.structure.ImportResult.java
protected void init(Document doc) { NodeList nlist = doc.getChildNodes(); Node rootNode = null;//from ww w. jav a 2s . c o m for (int i = 0; i < nlist.getLength(); i++) { Node item = nlist.item(i); if (ExportVisitor.WORKSPACE_EXPORT.equals(item.getNodeName())) rootNode = item; } node = new XMLNode("?", null); node.loadFromXMLNode(rootNode); }
From source file:org.openengsb.openengsbplugin.base.ConfiguredMojo.java
protected void tryExtractLicenseHeader(Document doc) { Node firstNode = doc.getChildNodes().item(0); if (firstNode.getNodeType() == Node.COMMENT_NODE) { LOG.trace(String.format("found license header with content:\n%s", firstNode.getNodeValue())); licenseHeaderComment = doc.removeChild(firstNode); }/* w w w .j a v a2s. com*/ }
From source file:org.openestate.io.core.XmlUtils.java
/** * Print nodes of a {@link Document} recursively to the local logger. * * @param doc/* ww w . ja v a 2 s. c om*/ * the document to print */ public static void printNodes(Document doc) { NodeList children = doc.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); if (node instanceof Element) printNode((Element) node); } }
From source file:org.openestate.io.core.XmlUtils.java
/** * Replace the namespace of any {@link Element} in a {@link Document}. * * @param doc//from w w w . j a va 2s .c o m * the document to update * * @param newNamespaceURI * the new namespace URI */ public static void replaceNamespace(Document doc, String newNamespaceURI) { NodeList children = doc.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { XmlUtils.replaceNamespace(doc, children.item(i), newNamespaceURI); } }
From source file:org.openestate.io.core.XmlUtils.java
/** * Replace all text values of a {@link Document} with CDATA values. * * @param doc// w w w .j a v a2 s. c o m * the document to update */ public static void replaceTextWithCData(Document doc) { NodeList children = doc.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { XmlUtils.replaceTextWithCData(doc, children.item(i)); } }
From source file:org.openxdata.server.export.rdbms.engine.DataBuilder.java
public List<DataQuery> buildData(Document schemaDocument, Document dataDocument, FormData formData, List<String> tableNames, boolean update) { Validate.notNull(schemaDocument, "schemaDocument cannot be null"); Validate.notNull(dataDocument, "dataDocument cannot be null"); List<DataQuery> statements = new ArrayList<DataQuery>(); NodeList nodes = dataDocument.getChildNodes(); if (nodes != null) { for (int index = 0; index < nodes.getLength();) { Node tableElement = nodes.item(index); if (tableElement != null && !(tableElement instanceof org.w3c.dom.Text) || tableElement instanceof org.w3c.dom.Comment) { Functions.cleanNode(tableElement); createStatements(update, schemaDocument, tableElement, formData, statements, null, tableNames); }/*from w ww .j a va2 s. c om*/ //for now we process the first child node in document break; } } return statements; }
From source file:org.sakaiproject.content.chh.dspace.ContentHostingHandlerImplDSpace.java
private static final Document extractAllPropsResponseFromSOAP(StringBuffer xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); if (db == null) return null; Document d = db.parse(new ByteArrayInputStream(xml.toString().getBytes())); if (d == null) return null; // the useful content is burried within 4 layers of enclosing XML junk NodeList nl = d.getChildNodes(); for (int i = 0; i < 4; ++i) for (int j = 0; (nl != null) && (j < nl.getLength()); ++j) if (nl.item(j).getNodeName() != null && !nl.item(j).getNodeName().equals("")) { nl = nl.item(j).getChildNodes(); break; }//w ww.j a v a 2 s . c o m if (nl == null) return null; // did not find the expected 4 layers of // wrapper crap String x = nl.item(0).getNodeValue(); if (x == null) return null; x.replaceAll("<", "<"); x.replaceAll(">", ">"); x.replaceAll(""", "\""); return db.parse(new ByteArrayInputStream(x.getBytes())); }
From source file:org.sakaiproject.content.chh.dspace.ContentHostingHandlerImplDSpace.java
private DSpaceItemInfo queryDSpaceFor(String endpoint, String base, String needle) { try {/*www .ja v a2 s . c om*/ /* PARSE XML RESPONSE... */ // This is a really horrible tree-walk but it is, at least, NOT // vulnerable to the whitespace in the XML being reformatted. // After *much* debate, we decided that this is no worse than other // ways of doing this! Document d = getDSpaceProps(endpoint, base, 1); if (d == null) return null; // find multistatus node Node node_multistatus = null; NodeList nl = d.getChildNodes(); for (int j = 0; j < nl.getLength(); ++j) if (nl.item(j).getNodeName() != null && nl.item(j).getNodeName().equals(LAYER1)) { node_multistatus = nl.item(j); break; } if (node_multistatus == null) return null; boolean foundMatch = false; // haven't found the named node so far DSpaceItemInfo dii = new DSpaceItemInfo(); // the return value // (unless we return // null) dii.displayname = needle; // will only be returned if we find the // name sought dii.endpoint = endpoint; // examine each response node, looking for ones which match the // string sought nl = node_multistatus.getChildNodes(); for (int j = 0; j < nl.getLength(); ++j) { // only interested in nodes with name="response" if (nl.item(j).getNodeName() == null || !nl.item(j).getNodeName().equals(LAYER2)) continue; Node node_response = nl.item(j); NodeList resources = node_response.getChildNodes(); // grab the resource handle String handle = null; for (int k = 0; k < resources.getLength(); ++k) if (resources.item(k).getNodeName() != null && resources.item(k).getNodeName().equals(HREF)) { handle = resources.item(k).getFirstChild().getNodeValue().trim(); break; } if (handle == null) continue; // skip this resource if it // doesn't have an 'href' node. // Communities and collections have a /-imploded handle // hierarchy. // The comm/coll's own handle is the last one. String[] handles = handle.split("/"); handle = handles[handles.length - 1]; // only interested in // the last one if (!handle.startsWith("bitstream_")) // this only affects // reading bitstreams // when queryDSpaceFor // is called for a // resource dii.handle = handle.replace("%24", "/").substring(4); else { if (handles.length > 1) dii.handle = handles[handles.length - 2].replace("%24", "/").substring(4); else continue; // a handle containing only "bitstream_x" is // not valid try { int indx; String numberPart = handle.substring("bitstream_".length()); if ((indx = numberPart.indexOf(".")) != -1) numberPart = numberPart.substring(0, indx); dii.bitstreamID = Integer.parseInt(numberPart); } catch (NumberFormatException e) { } } // now loop over the child nodes again looking at propstat // blocks... for (int k = 0; k < resources.getLength(); ++k) { if (resources.item(k).getNodeName() == null || !resources.item(k).getNodeName().equals(LAYER3)) continue; // only // want // 'propstat' // nodes NodeList propstats = resources.item(k).getChildNodes(); // first check whether status is OK (ignore all HTTP status // codes except 200) // no status node is assumed to mean OK. abort this propstat // if status is not OK. boolean status_ok = true; for (int l = 0; l < propstats.getLength(); ++l) if (propstats.item(l).getNodeName() != null && propstats.item(l).getNodeName().equals(STATUS) && !propstats.item(l).getFirstChild().getNodeValue().trim().equals(ST_OK)) { status_ok = false; break; } if (!status_ok) continue; // skip this propstat node // Look for child nodes with name 'prop', having nested // child called 'displayname'. // This is only complicated when the base handle is a DSpace // resource. There will // be child nodes (with handle=bitstream_<n>) and the same // display name as the resource. // We need to extract properties from the bitstream_1 child // (.._2 is the license). for (int l = 0; l < propstats.getLength(); ++l) { if (propstats.item(l).getNodeName() != null && propstats.item(l).getNodeName().equals(LAYER4)) { boolean recordFromThisChild = false; String lastmod = null, contentlength = null, contenttype = null, dstype = null; NodeList properties = propstats.item(l).getChildNodes(); for (int m = 0; m < properties.getLength(); ++m) if (properties.item(m).getNodeName() != null) { if (properties.item(m).getNodeName().equals(DISPNM) && properties.item(m) .getFirstChild().getNodeValue().trim().equals(needle)) { foundMatch = true; recordFromThisChild = true; } if (properties.item(m).getNodeName().equals(LSTMOD)) lastmod = properties.item(m).getFirstChild().getNodeValue().trim(); if (properties.item(m).getNodeName().equals(CNTLEN)) contentlength = properties.item(m).getFirstChild().getNodeValue().trim(); if (properties.item(m).getNodeName().equals(CNTTYP)) contenttype = properties.item(m).getFirstChild().getNodeValue().trim(); if (properties.item(m).getNodeName().equals(DSTYPE)) { Node n = properties.item(m).getFirstChild(); while (n != null) { String s = n.getNodeName(); if (!s.equals("#text")) { dstype = s; break; } n = n.getNextSibling(); } } } if (recordFromThisChild) { if (lastmod != null) dii.lastmodified = lastmod; if (contentlength != null) try { dii.contentLength = Long.parseLong(contentlength); } catch (NumberFormatException nfe) { } if (contenttype != null) dii.contentType = contenttype; if (dstype != null) dii.itemType = dstype; if (DSRESOURCES_AS_COLLECTIONS) return dii; } } } } } if (foundMatch) return dii; } catch (Exception e) { log.warn("Error in CHH DSpace mechanism: parse error: [" + e.toString() + "]"); } return null; // problem talking to DSpace or named resource has gone }
From source file:org.sakaiproject.content.chh.dspace.ContentHostingHandlerImplDSpace.java
/** * getVirtualContentEntity is the entry point for the virtual entity * resolution mechanism. This parses the XML of the real parent to locate * the parameters required to talk to a DSpace instance. * //from ww w .java 2s.c o m * @see org.sakaiproject.content.api.ContentHostingHandler#getVirtualContentEntity(org.sakaiproject.content.api.ContentEntity, * java.lang.String) */ public ContentEntity getVirtualContentEntity(ContentEntity edit, String finalId) { // Algorithm: get the mount point from the XML file represented by // 'edit' (the real parent) // construct a new ContentEntityDSpace and return it try { boolean searchable = false; byte[] xml = ((ContentResource) edit).getContent(); if (xml == null) return null; DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); if (db == null) return null; Document d = db.parse(new ByteArrayInputStream(xml)); if (d == null) return null; Node node_mountpoint = null; NodeList nl = d.getChildNodes(); for (int j = 0; j < nl.getLength(); ++j) if (nl.item(j).getNodeName() != null && nl.item(j).getNodeName().equals(XML_NODE_NAME)) { node_mountpoint = nl.item(j); break; } if (node_mountpoint == null) return null; Node node_endpoint = node_mountpoint.getAttributes().getNamedItem(XML_ATTRIBUTE_ENDPOINT); if (node_endpoint == null) return null; final String endpoint = node_endpoint.getNodeValue(); if (endpoint == null || endpoint.equals("")) return null; // invalid // mountpoint // specification Node node_basehandle = node_mountpoint.getAttributes().getNamedItem(XML_ATTRIBUTE_BASE); if (node_basehandle == null) return null; final String basehandle = node_basehandle.getNodeValue(); if (basehandle == null || basehandle.equals("")) return null; // invalid // mountpoint // specification Node node_searchable = node_mountpoint.getAttributes().getNamedItem(XML_ATTRIBUTE_SEARCHABLE); if (node_searchable != null) searchable = Boolean.parseBoolean(node_searchable.getNodeValue()); String relativePath = finalId.substring(edit.getId().length()); ContentEntityDSpace ceds = resolveDSpace(edit, endpoint, basehandle, relativePath, this, searchable); Edit ce = ceds.wrap(); if (ce == null) return null; // happens when the requested URL // requires a log on but the user is // not logged on return (ContentEntity) ce; } catch (Exception e) { log.warn("Content Hosting Handler DSpace: Invalid XML for the mountpoint [" + edit.getId() + "], exception was " + e.toString()); return (ContentEntity) (resolveDSpace(edit, "", "", "", this, false).wrap()); } }