List of usage examples for org.w3c.dom Element getTagName
public String getTagName();
From source file:com.cloudbees.sdk.CommandServiceImpl.java
private Plugin parsePluginFile(File file) throws FileNotFoundException, XPathExpressionException { InputStream inputStream = new FileInputStream(file); try {/*from w w w. j av a 2 s . com*/ InputSource input = new InputSource(inputStream); Document doc = XmlHelper.readXML(input); Plugin plugin = new Plugin(); Element e = doc.getDocumentElement(); if (e.getTagName().equalsIgnoreCase("plugin")) { if (e.hasAttribute("artifact")) plugin.setArtifact(e.getAttribute("artifact")); NodeList nodes = e.getChildNodes(); List<String> jars = new ArrayList<String>(); plugin.setJars(jars); List<CommandProperties> commands = new ArrayList<CommandProperties>(); plugin.setProperties(commands); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeName().equals("jar")) jars.add(node.getTextContent().trim()); else if (node.getNodeName().equals("command")) { CommandProperties commandProperties = new CommandProperties(); commandProperties.setGroup(getAttribute(node, "group")); commandProperties.setName(getAttribute(node, "name")); commandProperties.setPattern(getAttribute(node, "pattern")); commandProperties.setDescription(getAttribute(node, "description")); commandProperties.setClassName(getAttribute(node, "className")); String str = getAttribute(node, "experimental"); if (str != null) commandProperties.setExperimental(Boolean.parseBoolean(str)); str = getAttribute(node, "priority"); if (str != null) commandProperties.setPriority(Integer.parseInt(str)); commands.add(commandProperties); } } } return plugin; } finally { IOUtils.closeQuietly(inputStream); } }
From source file:greenfoot.export.mygame.MyGameClient.java
private void parseScenarioXml(ScenarioInfo info, InputStream xmlStream) throws IOException { try {//from w w w. java 2s .c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = dbf.newDocumentBuilder(); Document doc = dbuilder.parse(xmlStream); Element root = doc.getDocumentElement(); if (root == null || !root.getTagName().equals("scenario")) { return; } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node childNode = children.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) childNode; if (element.getTagName().equals("shortdescription")) { info.setShortDescription(element.getTextContent()); } else if (element.getTagName().equals("longdescription")) { info.setLongDescription(element.getTextContent()); } else if (element.getTagName().equals("taglist")) { info.setTags(parseTagListXmlElement(element)); } else if (element.getTagName().equals("webpage")) { info.setUrl(element.getTextContent()); } else if (element.getTagName().equals("hassource")) { info.setHasSource(element.getTextContent().equals("true")); } } } } catch (ParserConfigurationException pce) { // what the heck do we do with this? } catch (SAXException saxe) { } }
From source file:greenfoot.export.mygame.MyGameClient.java
/** * Get a list of commonly used tags from the server. * //from w w w.j a v a2 s .c o m * @throws UnknownHostException if the host is unknown * @throws org.apache.commons.httpclient.ConnectTimeoutException if the connection timed out * @throws IOException if some other I/O exception occurs */ public List<String> getCommonTags(String hostAddress, int maxNumberOfTags) throws UnknownHostException, IOException { HttpClient client = getHttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(20 * 1000); GetMethod getMethod = new GetMethod(hostAddress + "common-tags/" + maxNumberOfTags); int response = client.executeMethod(getMethod); if (response > 400) { throw new IOException("HTTP error response " + response + " from server."); } // found - now we can parse the response InputStream responseStream = getMethod.getResponseBodyAsStream(); try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = dbf.newDocumentBuilder(); Document doc = dbuilder.parse(responseStream); Element root = doc.getDocumentElement(); if (root == null || !root.getTagName().equals("taglist")) { return Collections.<String>emptyList(); } return parseTagListXmlElement(root); } catch (SAXException saxe) { } catch (ParserConfigurationException pce) { } finally { responseStream.close(); } return Collections.<String>emptyList(); }
From source file:org.openhmis.oauth2.OAuth2Utils.java
public void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) { NodeList child = null;// w w w . j a v a2 s .c o m if (element == null) { child = doc.getChildNodes(); } else { child = element.getChildNodes(); } for (int j = 0; j < child.getLength(); j++) { if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { org.w3c.dom.Element childElement = (org.w3c.dom.Element) child.item(j); if (childElement.hasChildNodes()) { System.out.println(childElement.getTagName() + " : " + childElement.getTextContent()); oauthResponse.put(childElement.getTagName(), childElement.getTextContent()); parseXMLDoc(childElement, null, oauthResponse); } } } }
From source file:io.spring.initializr.test.generator.PomAssert.java
private void parseProperties() { NodeList nodes;//w ww .ja v a 2 s . c o m try { nodes = this.eng.getMatchingNodes(createRootNodeXPath("properties/*"), this.doc); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); } for (int i = 0; i < nodes.getLength(); i++) { Node item = nodes.item(i); if (item instanceof Element) { Element element = (Element) item; this.properties.put(element.getTagName(), element.getTextContent()); } } }
From source file:com.streamsets.pipeline.stage.processor.xmlflattener.XMLFlatteningProcessor.java
private void processXML(Record record, boolean currentlyInRecord, XMLNode current, List<Record> recordList, Record originalRecord) throws DOMException { NodeList nodeList = current.element.getChildNodes(); // Preprocess the child Nodes to avoid having to do it each time we hit a new element. if (currentlyInRecord) { Set<String> nodes = new HashSet<>(); for (int i = 0; i < nodeList.getLength(); i++) { Node next = nodeList.item(i); if (next.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) next; // We already saw the same name once before. Add it to the current element's map. if (nodes.contains(e.getTagName()) && !current.nodeCounters.containsKey(e.getTagName())) { current.nodeCounters.put(e.getTagName(), 0); } else { nodes.add(e.getTagName()); }/*from ww w .jav a2s. c o m*/ } } } for (int i = 0; i < nodeList.getLength(); i++) { Node next = nodeList.item(i); // Text node - add it as a field, if we are currently in a record if (currentlyInRecord && next.getNodeType() == Node.TEXT_NODE) { String text = ((Text) next).getWholeText(); if (text.trim().isEmpty()) { continue; } // If we don't need to keep existing fields, just write // If we need to keep existing fields, overwrite only if the original field does not exist // If we need to keep existing fields, and the current record has the path, overwrite only if newFieldsOverwrite if (!keepExistingFields || !record.has(getPathPrefix() + current.prefix) || newFieldsOverwrite) { ensureOutputFieldExists(record); record.set(getPathPrefix() + current.prefix, Field.create(text)); } } else if (next.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) next; String tagName = element.getTagName(); String elementPrefix; // If we are not in a record and this is the delimiter, then create a new record to fill in. if (!currentlyInRecord && element.getNodeName().equals(recordDelimiter)) { // Create a new record elementPrefix = tagName; //Increment record count flattenerPerRecordCount++; Record newR; //Add the node index as sourceRecordIdPostFix for newly created/cloned record. if (keepExistingFields) { newR = getContext().cloneRecord(originalRecord, String.valueOf(flattenerPerRecordCount)); } else { newR = getContext().createRecord(originalRecord, String.valueOf(flattenerPerRecordCount)); newR.set(Field.create(new HashMap<String, Field>())); } ensureOutputFieldExists(newR); recordList.add(newR); addAttrs(newR, element, elementPrefix); processXML(newR, true, new XMLNode(element, tagName), recordList, record); } else { // This is not a record delimiter, or we are currently already in a record // Check if this is the first time we are seeing this prefix? // The map tracks the next index to append. If the map does not have the key, don't add an index. elementPrefix = current.prefix + fieldDelimiter + tagName; if (current.nodeCounters.containsKey(tagName)) { Integer nextCount = current.nodeCounters.get(tagName); elementPrefix = elementPrefix + "(" + nextCount.toString() + ")"; current.nodeCounters.put(tagName, nextCount + 1); } if (currentlyInRecord) { addAttrs(record, element, elementPrefix); } XMLNode nextNode = new XMLNode(element, elementPrefix); processXML(record, currentlyInRecord, nextNode, recordList, record); } } } }
From source file:net.sf.zekr.engine.bookmark.BookmarkSet.java
@SuppressWarnings("unchecked") private void _loadXml(BookmarkItem item, Node node) { NodeList nodeList = node.getChildNodes(); for (int i = 0; i < nodeList.getLength(); i++) { Node n = nodeList.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element e = (Element) n; BookmarkItem bi = new BookmarkItem(); if (e.getTagName().equals("folder")) { bi.setFolder(true);//from ww w . j a va2 s. co m bi.setName(e.getAttribute("name")); bi.setDescription(e.getAttribute("desc")); String id = nextItemId(); bi.setId(id); itemMap.put(id, bi); item.addChild(bi); _loadXml(bi, e); } else if (e.getTagName().equals("item")) { bi.setFolder(false); bi.setName(e.getAttribute("name")); try { bi.setLocations( CollectionUtils.fromString(e.getAttribute("data"), ",", QuranLocation.class)); } catch (Exception exc) { // logger.implicitLog(exc); logger.log(exc); } bi.setDescription(e.getAttribute("desc")); String id = nextItemId(); bi.setId(id); itemMap.put(id, bi); item.addChild(bi); } } } }
From source file:net.sf.jabref.importer.fetcher.GVKParser.java
private Element getChild(String name, Element e) { NodeList children = e.getChildNodes(); int j = children.getLength(); for (int i = 0; i < j; i++) { Node test = children.item(i); if (test.getNodeType() == Node.ELEMENT_NODE) { Element entry = (Element) test; if (entry.getTagName().equals(name)) { return entry; }// w w w.j a v a 2 s.com } } return null; }
From source file:net.sf.jabref.importer.fetcher.GVKParser.java
private List<Element> getChildren(String name, Element e) { List<Element> result = new LinkedList<>(); NodeList children = e.getChildNodes(); int j = children.getLength(); for (int i = 0; i < j; i++) { Node test = children.item(i); if (test.getNodeType() == Node.ELEMENT_NODE) { Element entry = (Element) test; if (entry.getTagName().equals(name)) { result.add(entry);//from w w w . j av a 2 s . c om } } } return result; }
From source file:com.redhat.plugin.eap6.EAP6DeploymentStructureMojo.java
protected void buildDeploymentStructure(Document doc, Map<Artifact, String> moduleMap, List<SubDeployment> subdeployments) throws MojoFailureException, XPathExpressionException { Element root = doc.getDocumentElement(); if (!root.getTagName().equals("jboss-deployment-structure")) throw new MojoFailureException("Root element is not jboss-deployment-structure"); Element deployment = (Element) xp_deployment.evaluate(doc, XPathConstants.NODE); if (deployment == null) { deployment = doc.createElement("deployment"); root.insertBefore(deployment, root.getFirstChild()); }/*from w w w . j a v a2 s. c o m*/ Element depDependencies = (Element) xp_dependencies.evaluate(deployment, XPathConstants.NODE); if (depDependencies == null) { depDependencies = doc.createElement("dependencies"); deployment.appendChild(depDependencies); } Collection<String> mods = moduleMap.values(); getLog().debug("From project-dependencies" + mods); fillModuleEntries(doc, depDependencies, mods); getLog().debug("Element <" + depDependencies.getTagName() + ">: " + depDependencies.getChildNodes().getLength() + " elements"); if (subdeployments != null && !subdeployments.isEmpty()) { for (SubDeployment sd : subdeployments) { XPathExpression xp = xpf.newXPath() .compile("/jboss-deployment-structure/sub-deployment [@name='" + sd.getName() + "']"); Element subEl = (Element) xp.evaluate(doc, XPathConstants.NODE); if (subEl == null) { getLog().debug("Creating sub-deployment-section for <" + sd.getName() + ">"); subEl = doc.createElement("sub-deployment"); root.appendChild(subEl); subEl.setAttribute("name", sd.getName()); } Element subDependencies = (Element) xp_dependencies.evaluate(subEl, XPathConstants.NODE); if (subDependencies == null) { subDependencies = doc.createElement("dependencies"); subEl.appendChild(subDependencies); } Set<String> modules = new HashSet<String>(); xp = xpf.newXPath().compile("/jboss-deployment-structure/deployment/dependencies/module/@name"); NodeList nl = (NodeList) xp.evaluate(sd.getDocument(), XPathConstants.NODESET); int n = nl.getLength(); for (int i = 0; i < n; i++) { if (moduleMap.values().contains(nl.item(i).getTextContent())) continue; modules.add(nl.item(i).getTextContent()); } getLog().debug("From sub-deployment <" + sd.getName() + ">:" + modules); fillModuleEntries(doc, subDependencies, modules); getLog().debug("Child-Elements for <" + subEl.getAttribute("name") + ">: " + subEl.getChildNodes().getLength()); getLog().debug("Element <" + subEl.getTagName() + "." + subDependencies.getTagName() + ">: " + subDependencies.getChildNodes().getLength() + " elements"); } } NodeList nlSub = (NodeList) xp_subdeployment.evaluate(doc, XPathConstants.NODESET); int nSub = nlSub.getLength(); getLog().debug("Retrieved subdeployment-sections (" + nSub + ")"); }