/**
   * add FlagImangesSet to Properties, which needn't to change the dir level, just ouput to the
   * ouput dir.
   *
   * @param prop job configuration
   * @param key list name
   * @param set relative flag image files
   */
  private void addFlagImagesSetToProperties(
      final Job prop, final String key, final Set<String> set) {
    String value = null;
    final Set<String> newSet = new LinkedHashSet<String>(INT_128);
    for (final String file : set) {
      if (new File(file).isAbsolute()) {
        // no need to append relative path before absolute paths
        newSet.add(FileUtils.normalize(file));
      } else {
        // In ant, all the file separator should be slash, so we need to
        // replace all the back slash with slash.
        newSet.add(
            FileUtils.separatorsToUnix(
                FileUtils.normalize(new StringBuffer().append(file).toString())));
      }
    }

    // write list attribute to file
    final String fileKey = key.substring(0, key.lastIndexOf("list")) + "file";
    prop.setProperty(fileKey, key.substring(0, key.lastIndexOf("list")) + ".list");
    final File list = new File(tempDir, prop.getProperty(fileKey));
    Writer bufferedWriter = null;
    try {
      bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(list)));
      final Iterator<String> it = newSet.iterator();
      while (it.hasNext()) {
        bufferedWriter.write(it.next());
        if (it.hasNext()) {
          bufferedWriter.write("\n");
        }
      }
      bufferedWriter.flush();
      bufferedWriter.close();
    } catch (final FileNotFoundException e) {
      logger.logException(e);
    } catch (final IOException e) {
      logger.logException(e);
    } finally {
      if (bufferedWriter != null) {
        try {
          bufferedWriter.close();
        } catch (final IOException e) {
          logger.logException(e);
        }
      }
    }

    value = StringUtils.assembleString(newSet, COMMA);

    prop.setProperty(key, value);

    // clear set
    set.clear();
    newSet.clear();
  }
 /**
  * Write map of sets to a file.
  *
  * <p>The serialization format is XML properties format where values are comma separated lists.
  *
  * @param m map to serialize
  * @param filename output filename
  */
 private void writeMapToXML(final Map<String, Set<String>> m, final String filename) {
   if (m == null) {
     return;
   }
   final Properties prop = new Properties();
   for (final Map.Entry<String, Set<String>> entry : m.entrySet()) {
     final String key = entry.getKey();
     final String value = StringUtils.assembleString(entry.getValue(), COMMA);
     prop.setProperty(key, value);
   }
   final File outputFile = new File(tempDir, filename);
   OutputStream os = null;
   try {
     os = new FileOutputStream(outputFile);
     prop.storeToXML(os, null);
     os.close();
   } catch (final IOException e) {
     this.logger.logException(e);
   } finally {
     if (os != null) {
       try {
         os.close();
       } catch (final Exception e) {
         logger.logException(e);
       }
     }
   }
 }
  /**
   * Write result files.
   *
   * @throws DITAOTException if writing result files failed
   */
  private void outputResult() throws DITAOTException {
    final File dir = new File(tempDir);
    if (!dir.exists()) {
      dir.mkdirs();
    }

    Job prop = null;
    try {
      prop = new Job(dir);
    } catch (final IOException e) {
      throw new DITAOTException("Failed to create empty job: " + e.getMessage(), e);
    }

    prop.setProperty(INPUT_DIR, baseInputDir);
    prop.setProperty(INPUT_DITAMAP, prefix + inputFile);

    prop.setProperty(INPUT_DITAMAP_LIST_FILE_LIST, USER_INPUT_FILE_LIST_FILE);
    final File inputfile = new File(tempDir, USER_INPUT_FILE_LIST_FILE);
    Writer bufferedWriter = null;
    try {
      bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(inputfile)));
      bufferedWriter.write(prefix + inputFile);
      bufferedWriter.flush();
    } catch (final FileNotFoundException e) {
      logger.logException(e);
    } catch (final IOException e) {
      logger.logException(e);
    } finally {
      if (bufferedWriter != null) {
        try {
          bufferedWriter.close();
        } catch (final IOException e) {
          logger.logException(e);
        }
      }
    }

    // add out.dita.files,tempdirToinputmapdir.relative.value to solve the
    // output problem
    relativeValue = prefix;
    formatRelativeValue = formatRelativeValue(relativeValue);
    prop.setProperty("tempdirToinputmapdir.relative.value", formatRelativeValue);

    prop.setProperty("uplevels", getUpdateLevels());
    addSetToProperties(prop, OUT_DITA_FILES_LIST, outDitaFilesSet);

    addSetToProperties(prop, FULL_DITAMAP_TOPIC_LIST, ditaSet);
    addSetToProperties(prop, FULL_DITA_TOPIC_LIST, fullTopicSet);
    addSetToProperties(prop, FULL_DITAMAP_LIST, fullMapSet);
    addSetToProperties(prop, HREF_DITA_TOPIC_LIST, hrefTopicSet);
    addSetToProperties(prop, CONREF_LIST, conrefSet);
    addSetToProperties(prop, IMAGE_LIST, imageSet);
    addSetToProperties(prop, FLAG_IMAGE_LIST, flagImageSet);
    addSetToProperties(prop, HTML_LIST, htmlSet);
    addSetToProperties(prop, HREF_TARGET_LIST, hrefTargetSet);
    addSetToProperties(prop, HREF_TOPIC_LIST, hrefWithIDSet);
    addSetToProperties(prop, CHUNK_TOPIC_LIST, chunkTopicSet);
    addSetToProperties(prop, SUBJEC_SCHEME_LIST, schemeSet);
    addSetToProperties(prop, CONREF_TARGET_LIST, conrefTargetSet);
    addSetToProperties(prop, COPYTO_SOURCE_LIST, copytoSourceSet);
    addSetToProperties(prop, SUBSIDIARY_TARGET_LIST, subsidiarySet);
    addSetToProperties(prop, CONREF_PUSH_LIST, conrefpushSet);
    addSetToProperties(prop, KEYREF_LIST, keyrefSet);
    addSetToProperties(prop, CODEREF_LIST, coderefSet);

    // @processing-role
    addSetToProperties(prop, RESOURCE_ONLY_LIST, resourceOnlySet);

    addFlagImagesSetToProperties(prop, REL_FLAGIMAGE_LIST, relFlagImagesSet);

    // Convert copyto map into set and output
    final Set<String> copytoSet = new HashSet<String>(INT_128);
    for (final Map.Entry<String, String> entry : copytoMap.entrySet()) {
      copytoSet.add(entry.toString());
    }
    addSetToProperties(prop, COPYTO_TARGET_TO_SOURCE_MAP_LIST, copytoSet);
    addKeyDefSetToProperties(prop, KEY_LIST, keysDefMap.values());

    try {
      logger.logInfo("Serializing job specification");
      prop.write();
    } catch (final IOException e) {
      throw new DITAOTException(
          "Failed to serialize job configuration files: " + e.getMessage(), e);
    }

    // Output relation-graph
    writeMapToXML(reader.getRelationshipGrap(), FILE_NAME_SUBJECT_RELATION);
    // Output topic-scheme dictionary
    writeMapToXML(this.schemeDictionary, FILE_NAME_SUBJECT_DICTIONARY);

    // added by Willam on 2009-07-17 for req #12014 start
    if (INDEX_TYPE_ECLIPSEHELP.equals(transtype)) {
      // Output plugin id
      final File pluginIdFile = new File(tempDir, FILE_NAME_PLUGIN_XML);
      final DelayConrefUtils delayConrefUtils = new DelayConrefUtils();
      delayConrefUtils.writeMapToXML(reader.getPluginMap(), pluginIdFile);
      // write the result into the file
      final StringBuffer result = reader.getResult();
      try {
        export.write(result.toString());
      } catch (final IOException e) {
        logger.logException(e);
      }
    }
    // added by Willam on 2009-07-17 for req #12014 end

  }
  private void processParseResult(String currentFile) {
    final Map<String, String> cpMap = reader.getCopytoMap();
    final Map<String, KeyDef> kdMap = reader.getKeysDMap();
    // Added by William on 2010-06-09 for bug:3013079 start
    // the reader's reset method will clear the map.
    final Map<String, String> exKdMap = reader.getExKeysDefMap();
    exKeyDefMap.putAll(exKdMap);
    // Added by William on 2010-06-09 for bug:3013079 end

    // Category non-copyto result and update uplevels accordingly
    for (final String file : reader.getNonCopytoResult()) {
      categorizeResultFile(file);
      updateUplevels(file);
    }

    // Update uplevels for copy-to targets, and store copy-to map.
    // Note: same key(target) copy-to will be ignored.
    for (final String key : cpMap.keySet()) {
      final String value = cpMap.get(key);

      if (copytoMap.containsKey(key)) {
        // edited by Alan on Date:2009-11-02 for Work Item:#1590 start
        /*
         * StringBuffer buff = new StringBuffer();
         * buff.append("Copy-to task [href=\""); buff.append(value);
         * buff.append("\" copy-to=\""); buff.append(key);
         * buff.append("\"] which points to another copy-to target");
         * buff.append(" was ignored.");
         * logger.logWarn(buff.toString());
         */
        final Properties prop = new Properties();
        prop.setProperty("%1", value);
        prop.setProperty("%2", key);
        logger.logWarn(MessageUtils.getMessage("DOTX065W", prop).toString());
        // edited by Alan on Date:2009-11-02 for Work Item:#1590 end
        ignoredCopytoSourceSet.add(value);
      } else {
        updateUplevels(key);
        copytoMap.put(key, value);
      }
    }
    // TODO Added by William on 2009-06-09 for scheme key bug(497)
    schemeSet.addAll(reader.getSchemeRefSet());

    // collect key definitions
    for (final String key : kdMap.keySet()) {
      // key and value.keys will differ when keydef is a redirect to another keydef
      final KeyDef value = kdMap.get(key);
      if (keysDefMap.containsKey(key)) {
        // if there already exists duplicated key definition in
        // different map files.
        // Should only emit this if in a debug mode; comment out for now
        /*
         * Properties prop = new Properties(); prop.put("%1", key);
         * prop.put("%2", value); prop.put("%3", currentFile); logger
         * .logInfo(MessageUtils.getMessage("DOTJ048I",
         * prop).toString());
         */
      } else {
        updateUplevels(key);
        // add the ditamap where it is defined.
        /*
         * try { keydef.write("<keydef ");
         * keydef.write("keys=\""+key+"\" ");
         * keydef.write("href=\""+value+"\" ");
         * keydef.write("source=\""+currentFile+"\"/>");
         * keydef.write("\n"); keydef.flush(); } catch (IOException e) {
         *
         * logger.logException(e); }
         */
        keysDefMap.put(key, new KeyDef(key, value.href, currentFile));
      }
      // TODO Added by William on 2009-06-09 for scheme key bug(532-547)
      // if the current file is also a schema file
      if (schemeSet.contains(currentFile)) {
        // write the keydef into the scheme keydef file
        try {
          schemekeydef.writeStartElement(ELEMENT_KEYDEF);
          schemekeydef.writeAttribute(ATTRIBUTE_KEYS, key);
          if (value.href != null) {
            schemekeydef.writeAttribute(ATTRIBUTE_HREF, value.href);
          }
          schemekeydef.writeAttribute(ATTRIUBTE_SOURCE, currentFile);
          schemekeydef.writeEndElement();
        } catch (final SAXException e) {
          logger.logException(e);
        }
      }
    }

    hrefTargetSet.addAll(reader.getHrefTargets());
    hrefWithIDSet.addAll(reader.getHrefTopicSet());
    chunkTopicSet.addAll(reader.getChunkTopicSet());
    // schemeSet.addAll(reader.getSchemeRefSet());
    conrefTargetSet.addAll(reader.getConrefTargets());
    nonConrefCopytoTargetSet.addAll(reader.getNonConrefCopytoTargets());
    ignoredCopytoSourceSet.addAll(reader.getIgnoredCopytoSourceSet());
    subsidiarySet.addAll(reader.getSubsidiaryTargets());
    outDitaFilesSet.addAll(reader.getOutFilesSet());
    resourceOnlySet.addAll(reader.getResourceOnlySet());

    // Generate topic-scheme dictionary
    if (reader.getSchemeSet() != null && reader.getSchemeSet().size() > 0) {
      Set<String> children = null;
      children = this.schemeDictionary.get(currentFile);
      if (children == null) {
        children = new HashSet<String>();
      }
      children.addAll(reader.getSchemeSet());
      // for Linux support
      currentFile = FileUtils.separatorsToUnix(currentFile);

      this.schemeDictionary.put(currentFile, children);
      final Set<String> hrfSet = reader.getHrefTargets();
      for (final String f : hrfSet) {
        // for Linux support
        final String filename = FileUtils.separatorsToUnix(f);

        children = this.schemeDictionary.get(filename);
        if (children == null) {
          children = new HashSet<String>();
        }
        children.addAll(reader.getSchemeSet());
        this.schemeDictionary.put(filename, children);
      }
    }
  }
  private void parseInputParameters(final AbstractPipelineInput input) {
    final String basedir = input.getAttribute(ANT_INVOKER_PARAM_BASEDIR);
    final String ditaInput = input.getAttribute(ANT_INVOKER_PARAM_INPUTMAP);

    tempDir = input.getAttribute(ANT_INVOKER_PARAM_TEMPDIR);
    ditaDir = input.getAttribute(ANT_INVOKER_EXT_PARAM_DITADIR);
    ditavalFile = input.getAttribute(ANT_INVOKER_PARAM_DITAVAL);
    xmlValidate = Boolean.valueOf(input.getAttribute(ANT_INVOKER_EXT_PARAM_VALIDATE));

    // Added by William on 2009-07-18 for req #12014 start
    // get transtype
    transtype = input.getAttribute(ANT_INVOKER_EXT_PARAM_TRANSTYPE);
    // Added by William on 2009-07-18 for req #12014 start

    gramcache = "yes".equalsIgnoreCase(input.getAttribute(ANT_INVOKER_EXT_PARAM_GRAMCACHE));
    setSystemid = "yes".equalsIgnoreCase(input.getAttribute(ANT_INVOKER_EXT_PARAN_SETSYSTEMID));

    // For the output control
    outputUtils = new OutputUtils();
    outputUtils.setGeneratecopyouter(input.getAttribute(ANT_INVOKER_EXT_PARAM_GENERATECOPYOUTTER));
    outputUtils.setOutterControl(input.getAttribute(ANT_INVOKER_EXT_PARAM_OUTTERCONTROL));
    outputUtils.setOnlyTopicInMap(input.getAttribute(ANT_INVOKER_EXT_PARAM_ONLYTOPICINMAP));

    // Set the OutputDir
    final File path = new File(input.getAttribute(ANT_INVOKER_EXT_PARAM_OUTPUTDIR));
    if (path.isAbsolute()) {
      outputUtils.setOutputDir(path.getAbsolutePath());
    } else {
      throw new IllegalArgumentException("Output directory " + tempDir + " must be absolute");
    }

    // Resolve relative paths base on the basedir.
    File inFile = new File(ditaInput);
    if (!inFile.isAbsolute()) {
      // XXX Shouldn't this be resolved to current directory, not Ant script base directory?
      inFile = new File(basedir, ditaInput);
    }
    try {
      inFile = inFile.getCanonicalFile();
    } catch (final IOException e1) {
      logger.logException(e1);
    }
    if (!new File(tempDir).isAbsolute()) {
      throw new IllegalArgumentException("Temporary directory " + tempDir + " must be absolute");
    } else {
      tempDir = FileUtils.normalize(tempDir);
    }
    if (!new File(ditaDir).isAbsolute()) {
      throw new IllegalArgumentException(
          "DITA-OT installation directory " + tempDir + " must be absolute");
    } else {
      ditaDir = FileUtils.normalize(ditaDir);
    }
    if (ditavalFile != null && !new File(ditavalFile).isAbsolute()) {
      // XXX Shouldn't this be resolved to current directory, not Ant script base directory?
      ditavalFile = new File(basedir, ditavalFile).getAbsolutePath();
    }

    baseInputDir = new File(inFile.getAbsolutePath()).getParent();
    baseInputDir = FileUtils.normalize(baseInputDir);

    rootFile = inFile.getAbsolutePath();
    rootFile = FileUtils.normalize(rootFile);

    inputFile = inFile.getName();
    try {
      // Added by William on 2009-06-09 for scheme key bug
      // create the keydef file for scheme files
      schemekeydef =
          XMLSerializer.newInstance(new FileOutputStream(new File(tempDir, "schemekeydef.xml")));
      schemekeydef.writeStartDocument();
      schemekeydef.writeStartElement(ELEMENT_STUB);

      // Added by William on 2009-06-25 for req #12014 start
      // create the export file for exportanchors
      // write the head
      export =
          new OutputStreamWriter(new FileOutputStream(new File(tempDir, FILE_NAME_EXPORT_XML)));
      export.write(XML_HEAD);
      export.write("<stub>");
      // Added by William on 2009-06-25 for req #12014 end
    } catch (final FileNotFoundException e) {
      logger.logException(e);
    } catch (final IOException e) {
      logger.logException(e);
    } catch (final SAXException e) {
      logger.logException(e);
    }

    // Set the mapDir
    outputUtils.setInputMapPathName(inFile.getAbsolutePath());
  }