Java Code Examples for org.w3c.dom.Document#hasChildNodes()

The following examples show how to use org.w3c.dom.Document#hasChildNodes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DTConfiguration.java    From Bats with Apache License 2.0 6 votes vote down vote up
public void loadFile(File file, Scope defaultScope) throws IOException, ParserConfigurationException, SAXException, ConfigException
{
  Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
  Element documentElement = doc.getDocumentElement();
  if (!documentElement.getNodeName().equals("configuration")) {
    throw new ConfigException("Root element needs to be \"configuration\"");
  }
  if (doc.hasChildNodes()) {
    NodeList propertyNodes = documentElement.getChildNodes();
    for (int i = 0; i < propertyNodes.getLength(); i++) {
      Node propertyNode = propertyNodes.item(i);
      if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
        if (propertyNode.getNodeName().equals("property")) {
          processPropertyNode((Element)propertyNode, defaultScope);
        } else {
          LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName());
        }
      }
    }
  }
}
 
Example 2
Source File: DTConfiguration.java    From attic-apex-core with Apache License 2.0 6 votes vote down vote up
public void loadFile(File file, Scope defaultScope) throws IOException, ParserConfigurationException, SAXException, ConfigException
{
  Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
  Element documentElement = doc.getDocumentElement();
  if (!documentElement.getNodeName().equals("configuration")) {
    throw new ConfigException("Root element needs to be \"configuration\"");
  }
  if (doc.hasChildNodes()) {
    NodeList propertyNodes = documentElement.getChildNodes();
    for (int i = 0; i < propertyNodes.getLength(); i++) {
      Node propertyNode = propertyNodes.item(i);
      if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
        if (propertyNode.getNodeName().equals("property")) {
          processPropertyNode((Element)propertyNode, defaultScope);
        } else {
          LOG.warn("Ignoring unknown element {}", propertyNode.getNodeName());
        }
      }
    }
  }
}
 
Example 3
Source File: BuildVersionService.java    From oxTrust with MIT License 6 votes vote down vote up
@PostConstruct
public void initalize() {
	try (InputStream is = getClass().getResourceAsStream("/META-INF/beans.xml")) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = factory.newDocumentBuilder();
           Document doc = dBuilder.parse(is);
		doc.getDocumentElement().normalize();
		log.info("Root element :" + doc.getDocumentElement().getNodeName());
		NodeList nList = doc.getElementsByTagName("bean");

		if (doc.hasChildNodes()) {
			readBuildDetails(nList);
		}
	} catch (Exception ex) {
		log.error("Failed to obtain build version", ex);
	}

}
 
Example 4
Source File: Reader.java    From EpubParser with Apache License 2.0 5 votes vote down vote up
private void parseContainerXml(DocumentBuilder docBuilder, Document document, ZipFile epubFile) throws ReadingException {
	if (document.hasChildNodes()) {
		isFoundNeeded = false;
		traverseDocumentNodesAndFillContent(document.getChildNodes(), content.getContainer());
	}

	String opfFilePath = content.getContainer().getFullPathValue();
	ZipEntry opfFileEntry = epubFile.getEntry(opfFilePath);

	if (opfFileEntry == null) {
		for (String entryName : content.getEntryNames()) {
			if (entryName.contains(Constants.EXTENSION_OPF)) {
				opfFileEntry = epubFile.getEntry(entryName);
				break;
			}
		}
	}

	if (opfFileEntry == null) {
		throw new ReadingException(".opf file not found");
	}

	InputStream opfFileInputStream;
	try {
		opfFileInputStream = epubFile.getInputStream(opfFileEntry);
	} catch (IOException e) {
		e.printStackTrace();
		throw new ReadingException("IO error while reading " + Constants.EXTENSION_OPF + " inputstream: " + e.getMessage());
	}

	Document packageDocument = getDocument(docBuilder, opfFileInputStream, Constants.EXTENSION_OPF);
	parseOpfFile(packageDocument, content.getPackage());
}
 
Example 5
Source File: DocProcessor.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
protected org.w3c.dom.Element getRootNode(final Document doc) {
	org.w3c.dom.Element root = null;
	if (doc.hasChildNodes()) {
		root = (org.w3c.dom.Element) doc.getElementsByTagName(getRootName()).item(0);
	}
	if (root == null) {
		root = doc.createElement(getRootName());
		doc.appendChild(root);
	}
	return root;
}
 
Example 6
Source File: DOMWriter.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
public void serializeDocument(Document document) throws XMLStreamException {
 
  String encoding = document.getInputEncoding();
  String version  = document.getXmlVersion();
  if (encoding != null) {
    serializer.writeStartDocument(encoding, version);
  } else {
    serializer.writeStartDocument(version);
  }
  if (document.hasChildNodes()) {
    serializeNodeList(document.getChildNodes());
  }
  serializer.writeEndDocument();
}
 
Example 7
Source File: CloneProfileTestCase.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Parse root node
 */
private List<String> findProfileSubsystemsInXML(Document doc, String profileName) {
    List<String> output = null;
    if (!doc.hasChildNodes()) {
        return output;
    }
    NodeList nodeList = doc.getChildNodes();
    return findProfileSubsystems(nodeList, profileName);
}
 
Example 8
Source File: Reader.java    From EpubParser with Apache License 2.0 4 votes vote down vote up
private void parseOpfFile(Document document, Package pckage) throws ReadingException {
	if (document.hasChildNodes()) {
		isFoundNeeded = false;
		traverseDocumentNodesAndFillContent(document.getChildNodes(), pckage);
	}
}
 
Example 9
Source File: Reader.java    From EpubParser with Apache License 2.0 4 votes vote down vote up
private void parseTocFile(Document document, Toc toc) throws ReadingException {
	if (document.hasChildNodes()) {
		isFoundNeeded = false;
		traverseDocumentNodesAndFillContent(document.getChildNodes(), toc);
	}
}
 
Example 10
Source File: ExamplesToTests.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public static void createTests(final ProcessorContext context, final Document doc) {
	if (doc == null || !doc.hasChildNodes()) { return; }
	final Document document = cleanDocumentTest(doc);
	createOperatorsTests(context, document, OPERATORS_TRANSFORMER);
}