Beispiel #1
0
  @Override
  public void read(final String filename) {
    if (matchList.isEmpty()) {
      throw new IllegalStateException("matchList not initialized");
    }

    match = false;
    needResolveEntity = true;
    inputFile = new File(filename);
    filePath = inputFile.getParent();
    inputFile.getPath();
    if (indexEntries.length() != 0) {
      // delete all the content in indexEntries
      indexEntries = new StringBuffer(INT_1024);
    }

    try {
      reader.setErrorHandler(new DITAOTXMLErrorHandler(filename, logger));
      final InputSource source =
          URIResolverAdapter.convertToInputSource(
              DitaURIResolverFactory.getURIResolver().resolve(filename, null));
      reader.parse(source);
    } catch (final Exception e) {
      logger.logException(e);
    }
  }
Beispiel #2
0
 /** Check whether the index entries we got is meaningful and valid */
 private static boolean verifyIndexEntries(final StringBuffer str) {
   if (str.length() == 0) {
     return false;
   }
   final int start = str.indexOf(GREATER_THAN); // start from first tag's end
   final int end = str.lastIndexOf(LESS_THAN); // end at last tag's start
   final String temp = str.substring(start + 1, end);
   if (temp.trim().length() != 0) {
     return true;
   }
   return false;
 }
Beispiel #3
0
  private void processChunk(final Element topicref, final File outputFile) {
    final String hrefValue = getValue(topicref, ATTRIBUTE_NAME_HREF);
    final Collection<String> chunkValue = split(getValue(topicref, ATTRIBUTE_NAME_CHUNK));
    final String copytoValue = getValue(topicref, ATTRIBUTE_NAME_COPY_TO);
    final String scopeValue = getCascadeValue(topicref, ATTRIBUTE_NAME_SCOPE);
    final String classValue = getValue(topicref, ATTRIBUTE_NAME_CLASS);
    final String processRoleValue = getCascadeValue(topicref, ATTRIBUTE_NAME_PROCESSING_ROLE);

    File outputFileName = outputFile;
    Writer tempWriter = null;
    Set<String> tempTopicID = null;

    targetTopicId = null;
    selectMethod = CHUNK_SELECT_DOCUMENT;
    include = false;

    boolean needWriteDitaTag = true;

    try {
      String parseFilePath;
      if (copytoValue != null && !chunkValue.contains(CHUNK_TO_CONTENT)) {
        if (getFragment(hrefValue) != null) {
          parseFilePath = setFragment(copytoValue, getFragment(hrefValue));
        } else {
          parseFilePath = copytoValue;
        }
      } else {
        parseFilePath = hrefValue;
      }

      // if @copy-to is processed in chunk module, the list file needs to
      // be updated.
      // Because @copy-to should be included in fulltopiclist, and the
      // source of coyy-to should be excluded in fulltopiclist.
      if (copytoValue != null && chunkValue.contains(CHUNK_TO_CONTENT) && hrefValue != null) {
        copyto.add(copytoValue);
        if (getFragment(hrefValue) != null) {
          copytoSource.add(stripFragment(hrefValue));
          copytotarget2source.put(toURI(copytoValue), toURI(stripFragment(hrefValue)));
        } else {
          copytoSource.add(hrefValue);
          copytotarget2source.put(toURI(copytoValue), toURI(hrefValue));
        }
      }

      if (parseFilePath != null && !ATTR_SCOPE_VALUE_EXTERNAL.equals(scopeValue)) {
        // now the path to target file make sense
        if (chunkValue.contains(CHUNK_TO_CONTENT)) {
          // if current element contains "to-content" in chunk
          // attribute
          // we need to create new buffer and flush the buffer to
          // file
          // after processing is finished
          tempWriter = output;
          tempTopicID = topicID;
          output = new StringWriter();
          topicID = new HashSet<String>();
          if (MAP_MAP.matches(classValue)) {
            // Very special case, we have a map element with
            // href value.
            // This is a map that needs to be chunked to
            // content.
            // No need to parse any file, just generate a stub
            // output.
            outputFileName = resolve(filePath, parseFilePath);
            needWriteDitaTag = false;
          } else if (copytoValue != null) {
            // use @copy-to value as the new file name
            outputFileName = resolve(filePath, copytoValue);
          } else if (hrefValue != null) {
            // try to use href value as the new file name
            if (chunkValue.contains(CHUNK_SELECT_TOPIC)
                || chunkValue.contains(CHUNK_SELECT_BRANCH)) {
              if (getFragment(hrefValue) != null) {
                // if we have an ID here, use it.
                outputFileName = resolve(filePath, getFragment(hrefValue) + FILE_EXTENSION_DITA);
              } else {
                // Find the first topic id in target file if
                // any.
                final String firstTopic = getFirstTopicId(resolve(filePath, hrefValue).getPath());
                if (firstTopic != null) {
                  outputFileName = resolve(filePath, firstTopic + FILE_EXTENSION_DITA);
                } else {
                  outputFileName = resolve(filePath, hrefValue);
                }
              }
            } else {
              // otherwise, use the href value instead
              outputFileName = resolve(filePath, hrefValue);
            }
          } else {
            // use randomly generated file name
            outputFileName = resolve(filePath, generateFilename());
          }

          // Check if there is any conflict
          if (outputFileName.exists() && !MAP_MAP.matches(classValue)) {
            final File t = outputFileName;
            outputFileName = resolve(filePath, generateFilename());
            conflictTable.put(outputFileName.getPath(), t.getPath());
          }
          // add newly generated file to changTable
          // the new entry in changeTable has same key and value
          // in order to indicate it is a newly generated file
          changeTable.put(outputFileName.getPath(), outputFileName.getPath());
        }
        // "by-topic" couldn't reach here
        this.outputFile = outputFileName;

        final String path = resolveTopic(filePath, parseFilePath);
        // FIXME: Should be URI
        String newpath;
        if (getFragment(path) != null) {
          newpath = setFragment(outputFileName.getPath(), getFragment(path));
        } else {
          final String firstTopicID = getFirstTopicId(path);
          if (firstTopicID != null) {
            newpath = setFragment(outputFileName.getPath(), firstTopicID);
          } else {
            newpath = outputFileName.getPath();
          }
        }
        // add file name changes to changeTable, this will be
        // used in
        // TopicRefWriter's updateHref method, very important!!!
        changeTable.put(path, newpath);
        // update current element's @href value
        topicref.setAttribute(
            ATTRIBUTE_NAME_HREF,
            getRelativeUnixPath(filePath + UNIX_SEPARATOR + FILE_NAME_STUB_DITAMAP, newpath));

        if (getFragment(parseFilePath) != null) {
          targetTopicId = getFragment(parseFilePath);
        }

        final String s = getChunkByToken(chunkValue, "select-", null);
        if (s != null) {
          selectMethod = s;
          // if the current topic href referred to a entire
          // topic file,it will be handled in "document" level.
          if (targetTopicId == null) {
            selectMethod = CHUNK_SELECT_DOCUMENT;
          }
        }
        final File tempPath = currentParsingFile;
        currentParsingFile = resolve(filePath, parseFilePath);

        if (!ATTR_PROCESSING_ROLE_VALUE_RESOURCE_ONLY.equals(processRoleValue)) {
          currentParsingFileTopicIDChangeTable = new HashMap<String, String>();
          // TODO recursive point
          reader.parse(currentParsingFile.toURI().toString());
          if (currentParsingFileTopicIDChangeTable.size() > 0) {
            final URI href = toURI(topicref.getAttribute(ATTRIBUTE_NAME_HREF));
            final String pathtoElem = href.getFragment() != null ? href.getFragment() : "";
            final String old_elementid =
                pathtoElem.contains(SLASH)
                    ? pathtoElem.substring(0, pathtoElem.indexOf(SLASH))
                    : pathtoElem;
            if (!old_elementid.isEmpty()) {
              final String new_elementid = currentParsingFileTopicIDChangeTable.get(old_elementid);
              if (new_elementid != null && !new_elementid.isEmpty()) {
                topicref.setAttribute(
                    ATTRIBUTE_NAME_HREF, setFragment(href, new_elementid).toString());
              }
            }
          }
          currentParsingFileTopicIDChangeTable = null;
        }
        // restore the currentParsingFile
        currentParsingFile = tempPath;
      }

      if (topicref.hasChildNodes()) {
        // if current element has child nodes and chunk results for
        // this element has value
        // which means current element makes sense for chunk action.
        final StringWriter tempOutput = (StringWriter) output;
        output = new StringWriter();
        final NodeList children = topicref.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
          final Node current = children.item(i);
          if (MAP_TOPICREF.matches(current)) {
            processChunk((Element) current, outputFileName);
          }
        }

        // merge results
        final StringBuffer parentResult = tempOutput.getBuffer();
        final CharSequence tmpContent = ((StringWriter) output).getBuffer();
        // Skip empty parents and @processing-role='resource-only' entries.
        // append into root topic
        if (parentResult.length() > 0
            && parseFilePath != null
            && !ATTR_PROCESSING_ROLE_VALUE_RESOURCE_ONLY.equals(processRoleValue)) {
          insertAfter(hrefValue, parentResult, tmpContent);
          // replace contents
        } else {
          parentResult.append(tmpContent);
        }
        // restore back to parent's output this is a different temp
        output = tempOutput;
      }

      if (chunkValue.contains(CHUNK_TO_CONTENT)) {
        final String tmpContent = ((StringWriter) output).toString();
        writeToContentChunk(tmpContent, outputFileName, needWriteDitaTag);
        // restore back original output
        output = tempWriter;
        topicID = tempTopicID;
      }
    } catch (final RuntimeException e) {
      throw e;
    } catch (final Exception e) {
      logger.error(e.getMessage(), e);
    }
  }