use of javax.xml.parsers.DocumentBuilderFactory in project OpenAttestation by OpenAttestation.
the class ReadXmlTest method getTagSelectionFromXml.
public TagSelection getTagSelectionFromXml(String xml) throws ParserConfigurationException, SAXException, IOException {
TagSelection ret = new TagSelection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
ArrayList<tag> tagList = new ArrayList<tag>();
int cnt = 0;
NodeList nodeList = doc.getElementsByTagName("attribute");
for (int s = 0; s < nodeList.getLength(); s++) {
Node fstNode = nodeList.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
String idValue = fstElmnt.getAttribute("oid");
Element lstNmElmnt = (Element) nodeList.item(cnt++);
NodeList lstNm = lstNmElmnt.getChildNodes();
String currentAction = ((Node) lstNm.item(0)).getNodeValue();
if (currentAction != null) {
tagList.add(new tag("", idValue, currentAction));
}
}
}
nodeList = doc.getElementsByTagName("selection");
Node fstNode = nodeList.item(0);
Element e = (Element) fstNode;
ret.id = e.getAttribute("id");
ret.name = e.getAttribute("name");
ret.tagList = tagList;
return ret;
}
use of javax.xml.parsers.DocumentBuilderFactory in project OpenGrok by OpenGrok.
the class SubversionRepository method setDirectoryName.
@Override
public void setDirectoryName(String directoryName) {
super.setDirectoryName(directoryName);
if (isWorking()) {
// set to true if we manage to find the root directory
Boolean rootFound = Boolean.FALSE;
List<String> cmd = new ArrayList<>();
cmd.add(RepoCommand);
cmd.add("info");
cmd.add("--xml");
File directory = new File(getDirectoryName());
Executor executor = new Executor(cmd, directory);
if (executor.exec() == 0) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(executor.getOutputStream());
String url = getValue(document.getElementsByTagName("url").item(0));
if (url == null) {
LOGGER.log(Level.WARNING, "svn info did not contain an URL for [{0}]. Assuming remote repository.", directoryName);
setRemote(true);
} else {
if (!url.startsWith("file")) {
setRemote(true);
}
}
String root = getValue(document.getElementsByTagName("root").item(0));
if (url != null && root != null) {
reposPath = url.substring(root.length());
rootFound = Boolean.TRUE;
}
} catch (SAXException saxe) {
LOGGER.log(Level.WARNING, "Parser error parsing svn output", saxe);
} catch (ParserConfigurationException pce) {
LOGGER.log(Level.WARNING, "Parser configuration error parsing svn output", pce);
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "IOException reading from svn process", ioe);
}
} else {
LOGGER.log(Level.WARNING, "Failed to execute svn info for [{0}]. Repository disabled.", directoryName);
}
setWorking(rootFound);
}
}
use of javax.xml.parsers.DocumentBuilderFactory in project OpenGrok by OpenGrok.
the class DirectoryListingTest method directoryListing.
/**
* Test directory listing
* @throws java.lang.Exception if an error occurs while generating the
* list.
*/
@Test
public void directoryListing() throws Exception {
StringWriter out = new StringWriter();
out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<start>\n");
DirectoryListing instance = new DirectoryListing();
instance.listTo("ctx", directory, out, directory.getPath(), Arrays.asList(directory.list()));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
assertNotNull("DocumentBuilderFactory is null", factory);
DocumentBuilder builder = factory.newDocumentBuilder();
assertNotNull("DocumentBuilder is null", builder);
out.append("</start>\n");
String str = out.toString();
System.out.println(str);
Document document = builder.parse(new ByteArrayInputStream(str.getBytes()));
NodeList nl = document.getElementsByTagName("tr");
int len = nl.getLength();
// add one extra for header and one for parent directory link
assertEquals(entries.length + 2, len);
// Skip the the header and parent link
for (int i = 2; i < len; ++i) {
validateEntry((Element) nl.item(i));
}
}
use of javax.xml.parsers.DocumentBuilderFactory in project OpenMEAP by OpenMEAP.
the class XmlUtils method getDocument.
/**
* Convenience method to parse an input stream into a document
* @param inputStream
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static Document getDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(inputStream);
}
use of javax.xml.parsers.DocumentBuilderFactory in project atlas by alibaba.
the class Configuration method readXmlConfig.
/**
* read args from xml
**/
void readXmlConfig(File xmlConfigFile) throws IOException, ParserConfigurationException, SAXException {
if (!xmlConfigFile.exists()) {
return;
}
System.out.printf("reading config file, %s\n", xmlConfigFile.getAbsolutePath());
BufferedInputStream input = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
input = new BufferedInputStream(new FileInputStream(xmlConfigFile));
InputSource source = new InputSource(input);
factory.setNamespaceAware(false);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(source);
NodeList issues = document.getElementsByTagName(TAG_ISSUE);
for (int i = 0, count = issues.getLength(); i < count; i++) {
Node node = issues.item(i);
Element element = (Element) node;
String id = element.getAttribute(ATTR_ID);
if (id.length() == 0) {
System.err.println("Invalid config file: Missing required issue id attribute");
continue;
}
if (id.equals(PROPERTY_ISSUE)) {
readPropertyFromXml(node);
} else if (id.equals(DEX_ISSUE)) {
readDexPatternsFromXml(node);
} else if (id.equals(SO_ISSUE)) {
readLibPatternsFromXml(node);
} else if (id.equals(RES_ISSUE)) {
readResPatternsFromXml(node);
} else if (id.equals(PACKAGE_CONFIG_ISSUE)) {
readPackageConfigFromXml(node);
} else if (id.equals(SIGN_ISSUE)) {
if (mUseSignAPk) {
readSignFromXml(node);
}
} else {
System.err.println("unknown issue " + id);
}
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
System.exit(-1);
}
}
}
}
Aggregations