/**
   * check whether the href/id element defined by keys has been exported.
   *
   * @param href href
   * @param id id
   * @param key keyname
   * @param tempDir absolute path to temporary director
   * @return result list
   */
  public List<Boolean> checkExport(
      String href, final String id, final String key, final File tempDir) {
    // parsed export .xml to get exported elements
    final File exportFile = new File(tempDir, FILE_NAME_EXPORT_XML);

    boolean idExported = false;
    boolean keyrefExported = false;
    try {
      // load export.xml only once
      if (root == null) {
        final DocumentBuilder builder = XMLUtils.getDocumentBuilder();
        builder.setEntityResolver(CatalogUtils.getCatalogResolver());
        root = builder.parse(new InputSource(new FileInputStream(exportFile)));
      }
      // get file node which contains the export node
      final Element fileNode = searchForKey(root.getDocumentElement(), href, "file");
      if (fileNode != null) {
        // iterate the child nodes
        final NodeList pList = fileNode.getChildNodes();
        for (int j = 0; j < pList.getLength(); j++) {
          final Node node = pList.item(j);
          if (Node.ELEMENT_NODE == node.getNodeType()) {
            final Element child = (Element) node;
            // compare keys
            if (child.getNodeName().equals("keyref")
                && child.getAttribute(ATTRIBUTE_NAME_NAME).equals(key)) {
              keyrefExported = true;
              // compare topic id
            } else if (child.getNodeName().equals("topicid")
                && child.getAttribute(ATTRIBUTE_NAME_NAME).equals(id)) {
              idExported = true;
              // compare element id
            } else if (child.getNodeName().equals("id")
                && child.getAttribute(ATTRIBUTE_NAME_NAME).equals(id)) {
              idExported = true;
            }
          }
          if (idExported && keyrefExported) {
            break;
          }
        }
      }
    } catch (final Exception e) {
      e.printStackTrace();
    }
    final List<Boolean> list = new ArrayList<>();
    list.add(idExported);
    list.add(keyrefExported);
    return list;
  }
Exemple #2
0
 /**
  * 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;
  }