/** * Write map into xml file. * * @param m map * @param outputFile output xml file */ public void writeMapToXML(final Map<String, Set<String>> m, final File outputFile) { if (m == null) { return; } final Properties prop = new Properties(); for (Map.Entry<String, Set<String>> entry : m.entrySet()) { final String key = entry.getKey(); final String value = StringUtils.join(entry.getValue(), COMMA); prop.setProperty(key, value); } // File outputFile = new File(tempDir, filename); final DocumentBuilder db = XMLUtils.getDocumentBuilder(); final Document doc = db.newDocument(); final Element properties = (Element) doc.appendChild(doc.createElement("properties")); final Set<Object> keys = prop.keySet(); for (Object key1 : keys) { final String key = (String) key1; final Element entry = (Element) properties.appendChild(doc.createElement("entry")); entry.setAttribute("key", key); entry.appendChild(doc.createTextNode(prop.getProperty(key))); } final TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; try { t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); } catch (final TransformerConfigurationException tce) { throw new RuntimeException(tce); } final DOMSource doms = new DOMSource(doc); OutputStream out = null; try { out = new FileOutputStream(outputFile); final StreamResult sr = new StreamResult(out); t.transform(doms, sr); } catch (final Exception e) { logger.error("Failed to process map: " + e.getMessage(), e); } finally { if (out != null) { try { out.close(); } catch (final IOException e) { logger.error("Failed to close output stream: " + e.getMessage()); } } } }
/** * Get the first topic id. * * @param path file path * @param dir file dir * @param useCatalog whether use catalog file for validation * @return topic id */ public static String getFirstTopicId(final URI path, final File dir, final boolean useCatalog) { if (path == null && dir == null) { return null; } final DITAOTLogger logger = new DITAOTJavaLogger(); final StringBuilder firstTopicId = new StringBuilder(); final TopicIdParser parser = new TopicIdParser(firstTopicId); try { final XMLReader reader = XMLUtils.getXMLReader(); reader.setContentHandler(parser); if (useCatalog) { reader.setEntityResolver(CatalogUtils.getCatalogResolver()); } reader.parse(dir.toURI().resolve(path).toString()); } catch (final Exception e) { logger.error(e.getMessage(), e); } return firstTopicId.toString(); }
/** * Find whether an id is refer to a topic in a dita file. * * @param absolutePathToFile the absolute path of dita file * @param id topic id * @return true if id find and false otherwise */ public boolean findTopicId(final File absolutePathToFile, final String id) { if (!absolutePathToFile.exists()) { return false; } try { // load the file final DocumentBuilder builder = XMLUtils.getDocumentBuilder(); builder.setEntityResolver(CatalogUtils.getCatalogResolver()); final Document root = builder.parse(new InputSource(new FileInputStream(absolutePathToFile))); // get root element final Element doc = root.getDocumentElement(); // do BFS final Queue<Element> queue = new LinkedList<>(); queue.offer(doc); while (!queue.isEmpty()) { final Element pe = queue.poll(); final NodeList pchildrenList = pe.getChildNodes(); for (int i = 0; i < pchildrenList.getLength(); i++) { final Node node = pchildrenList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { queue.offer((Element) node); } } final String classValue = pe.getAttribute(ATTRIBUTE_NAME_CLASS); if (classValue != null && TOPIC_TOPIC.matches(classValue)) { // topic id found if (pe.getAttribute(ATTRIBUTE_NAME_ID).equals(id)) { return true; } } } return false; } catch (final Exception e) { logger.error("Failed to read document: " + e.getMessage(), e); } return false; }