Example #1
0
  /**
   * Search specific element by key and tagName.
   *
   * @param root root element
   * @param key search keyword
   * @param tagName search tag name
   * @return search result, null of either input is invalid or the looking result is not found.
   */
  private Element searchForKey(final Element root, final String key, final String tagName) {
    if (root == null || StringUtils.isEmptyString(key)) {
      return null;
    }
    final Queue<Element> queue = new LinkedList<>();
    queue.offer(root);

    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);
        }
      }
      String value = pe.getNodeName();
      if (StringUtils.isEmptyString(value) || !value.equals(tagName)) {
        continue;
      }

      value = pe.getAttribute(ATTRIBUTE_NAME_NAME);
      if (StringUtils.isEmptyString(value)) {
        continue;
      }

      if (value.equals(key)) {
        return pe;
      }
    }
    return null;
  }
Example #2
0
  /**
   * 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());
        }
      }
    }
  }
Example #3
0
  /**
   * Parse the catalog file to get catalog map.
   *
   * @param ditaDir absolute path to directory to find catalog-dita.xml
   * @return catalog map
   * @deprecated use Apache Commons Catalog Resolver instead
   */
  @Deprecated
  public static synchronized HashMap<String, String> getCatalog(final File ditaDir) {
    if (map != null) {
      return map;
    }

    final File catalogFilePath =
        (ditaDir == null) ? new File(FILE_NAME_CATALOG) : new File(ditaDir, FILE_NAME_CATALOG);

    map = new HashMap<String, String>();
    final CatalogParser parser = new CatalogParser(map, ditaDir.getAbsolutePath());
    try {
      final XMLReader reader = StringUtils.getXMLReader();
      reader.setContentHandler(parser);
      reader.parse(catalogFilePath.toURI().toASCIIString());
    } catch (final Exception e) {
      logger.logException(e);
    }

    return map;
  }