Exemplo n.º 1
0
  @Test
  public void testMergeMap() throws Exception {
    final File filename = new File(srcDir, "merged.xml");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    final Map<String, String> exp = new HashMap<String, String>();
    exp.put(
        "toner-specs",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-specs\" href=\"toner-type-a-specs.dita\"/>");
    exp.put(
        "toner-handling",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-handling\" href=\"toner-type-b-handling.dita\"/>");
    exp.put(
        "toner-disposal",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-disposal\" href=\"toner-type-c-disposal.dita\"/>");

    TestUtils.resetXMLUnit();
    XMLUnit.setIgnoreWhitespace(true);
    assertEquals(exp.keySet(), act.keySet());
    for (Map.Entry<String, String> e : exp.entrySet()) {
      final Document ev = keyDefToDoc(e.getValue());
      final Document av = act.get(e.getKey()).element.getOwnerDocument();
      assertXMLEqual(ev, av);
    }
  }
Exemplo n.º 2
0
  @BeforeClass
  public static void setUp() throws IOException {
    tempDir = TestUtils.createTempDir(TestConrefPushParser.class);

    inputFile = new File(tempDir, "conrefpush_stub.xml");
    FileUtils.copyFile(new File(srcDir, "conrefpush_stub.xml"), inputFile);
    FileUtils.copyFile(
        new File(srcDir, "conrefpush_stub2.xml"), new File(tempDir, "conrefpush_stub2.xml"));
  }
  @Before
  public void setUp() throws IOException, DITAOTException {
    tempDir = TestUtils.createTempDir(getClass());

    inputDir = new File(resourceDir, "input");
    final File inputMap = new File(inputDir, "maps" + File.separator + "root-map-01.ditamap");
    final File outDir = new File(tempDir, "out");
    tmpDir = new File(tempDir, "temp");
    TestUtils.copy(new File(resourceDir, "temp"), tmpDir);
    final File ditaList = new File(tmpDir, "dita.list");
    final Properties props = readProperties(ditaList);
    props.put("user.input.dir", inputDir.getAbsolutePath());
    writeProperties(props, ditaList, false);
    writeProperties(props, new File(tmpDir, "dita.xml.properties"), true);

    DITAOTFileLogger.getInstance().setLogDir(tmpDir.getAbsolutePath());
    DITAOTFileLogger.getInstance()
        .setLogFile(DebugAndFilterModuleTest.class.getSimpleName() + ".log");
    DitaURIResolverFactory.setPath(tmpDir.getAbsolutePath());

    final PipelineHashIO pipelineInput = new PipelineHashIO();
    pipelineInput.setAttribute("inputmap", inputMap.getPath());
    pipelineInput.setAttribute("basedir", inputDir.getAbsolutePath());
    pipelineInput.setAttribute("inputdir", inputDir.getPath());
    pipelineInput.setAttribute("outputdir", outDir.getPath());
    pipelineInput.setAttribute("tempDir", tmpDir.getPath());
    pipelineInput.setAttribute("ditadir", ditaDir.getAbsolutePath());
    pipelineInput.setAttribute("ditaext", ".xml");
    pipelineInput.setAttribute("indextype", "xhtml");
    pipelineInput.setAttribute("encoding", "en-US");
    pipelineInput.setAttribute("targetext", ".html");
    pipelineInput.setAttribute("validate", "false");
    pipelineInput.setAttribute("generatecopyouter", "1");
    pipelineInput.setAttribute("outercontrol", "warn");
    pipelineInput.setAttribute("onlytopicinmap", "false");
    pipelineInput.setAttribute("ditalist", new File(tmpDir, "dita.list").getPath());
    pipelineInput.setAttribute("maplinks", new File(tmpDir, "maplinks.unordered").getPath());
    pipelineInput.setAttribute(Constants.ANT_INVOKER_EXT_PARAN_SETSYSTEMID, "yes");

    final AbstractFacade facade = new PipelineFacade();
    facade.setLogger(new TestUtils.TestLogger());
    facade.execute("DebugAndFilter", pipelineInput);
  }
Exemplo n.º 4
0
  @Test
  public void testKeyrefReader() throws Exception {
    final File filename = new File(srcDir, "keyrefreader.xml");

    //        final Set <String> set = new HashSet<String> ();
    //        set.add("blatview");
    //        set.add("blatfeference");
    //        set.add("blatintro");
    //        set.add("keyword");
    //        set.add("escape");
    //        set.add("top");
    //        set.add("nested");
    final KeyrefReader keyrefreader = new KeyrefReader();
    //        keyrefreader.setKeys(set);
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    final Map<String, String> exp = new HashMap<String, String>();
    exp.put(
        "blatfeference",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "blatview",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "blatintro",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "keyword",
        "<topicref keys='keyword' class='- map/topicref '><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>keyword value</keyword></keywords></topicmeta></topicref>");
    exp.put(
        "escape",
        "<topicref keys='escape' class='- map/topicref ' navtitle='&amp;&lt;&gt;&quot;&apos;'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>&amp;&lt;&gt;&quot;&apos;</keyword></keywords></topicmeta></topicref>");
    exp.put(
        "top",
        "<topicref keys='top' class='- map/topicref ' navtitle='top'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>top keyword</keyword></keywords></topicmeta><topicref keys='nested' class='- map/topicref ' navtitle='nested'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>nested keyword</keyword></keywords></topicmeta></topicref></topicref>");
    exp.put(
        "nested",
        "<topicref keys='nested' class='- map/topicref ' navtitle='nested'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>nested keyword</keyword></keywords></topicmeta></topicref>");

    TestUtils.resetXMLUnit();
    XMLUnit.setIgnoreWhitespace(true);
    assertEquals(exp.keySet(), act.keySet());
    for (Map.Entry<String, String> e : exp.entrySet()) {
      final Document ev = keyDefToDoc(e.getValue());
      final Document av = act.get(e.getKey()).element.getOwnerDocument();
      assertXMLEqual(ev, av);
    }
  }
Exemplo n.º 5
0
public class TestListReader {

  private static final File resourceDir = TestUtils.getResourceDir(TestListReader.class);

  @Test
  public void testread() {
    final File path = new File(resourceDir, "xhtml" + File.separator);
    DitaURIResolverFactory.setPath(path.getAbsolutePath());
    final ListReader listreader = new ListReader();
    // final String filename = "dita.xml.properties";
    listreader.read(null);
    final String userinputfile =
        "C:"
            + UNIX_SEPARATOR
            + "DITA-OT1.5"
            + UNIX_SEPARATOR
            + "SAXONIBMJDK"
            + UNIX_SEPARATOR
            + "testcase"
            + UNIX_SEPARATOR
            + "12014"
            + UNIX_SEPARATOR
            + ".."
            + UNIX_SEPARATOR
            + ".."
            + UNIX_SEPARATOR
            + "testdata"
            + UNIX_SEPARATOR
            + "12014";
    assertEquals(userinputfile, listreader.getContent().getValue().toString());
    final String userinputmap = "map1.ditamap";
    assertEquals(userinputmap, listreader.getInputMap());
    final String subjectschemelist = "[cvf.ditamap]";
    assertEquals(subjectschemelist, listreader.getSchemeSet().toString());
  }
}
Exemplo n.º 6
0
public class TestConrefPushParser {

  private static final File resourceDir = TestUtils.getResourceDir(TestConrefPushParser.class);
  private static final File srcDir = new File(resourceDir, "src");
  private static File tempDir;
  private static File inputFile;

  @BeforeClass
  public static void setUp() throws IOException {
    tempDir = TestUtils.createTempDir(TestConrefPushParser.class);

    inputFile = new File(tempDir, "conrefpush_stub.xml");
    FileUtils.copyFile(new File(srcDir, "conrefpush_stub.xml"), inputFile);
    FileUtils.copyFile(
        new File(srcDir, "conrefpush_stub2.xml"), new File(tempDir, "conrefpush_stub2.xml"));
  }

  @Test
  public void testWrite()
      throws DITAOTException, ParserConfigurationException, SAXException, IOException {
    /*
     * the part of content of conrefpush_stub2.xml is
     * <ol>
     * 	<li id="A">A</li>
     * 	<li id="B">B</li>
     * 	<li id="C">C</li>
     * </ol>
     *
     * the part of content of conrefpush_stup.xml is
     *  <steps>
     * 	 <step conaction="pushbefore"><cmd>before</cmd></step>
     *   <step conref="conrefpush_stub2.xml#X/A" conaction="mark"/>
     *   <step conref="conrefpush_stub2.xml#X/B" conaction="mark"/>
     *	 <step conaction="pushafter"><cmd>after</cmd></step>
     *	 <step conref="conrefpush_stub2.xml#X/C" conaction="pushreplace"><cmd>replace</cmd></step>
     *	</steps>
     *
     * after conrefpush the part of conrefpush_stub2.xml should be like this
     * <ol class="- topic/ol ">
     *  <li class="- topic/li task/step ">
     *  	<ph class="- topic/ph task/cmd ">
     *  	before
     *  	</ph>
     *  </li>
     *  <li id="A" class="- topic/li ">A</li>
     *	<li id="B" class="- topic/li ">B</li>
     *	<li class="- topic/li task/step ">
     *		<ph class="- topic/ph task/cmd ">
     *		after
     *		</ph>
     *	</li>
     *	<li class="- topic/li task/step ">
     *		<ph class="- topic/ph task/cmd ">
     *		replace
     *		</ph>
     *	</li>
     * </ol>
     */
    final ConrefPushParser parser = new ConrefPushParser();
    final ConrefPushReader reader = new ConrefPushReader();

    reader.read(inputFile.getAbsolutePath());
    final Map<String, Hashtable<String, String>> pushSet = reader.getPushMap();
    final Iterator<Map.Entry<String, Hashtable<String, String>>> iter =
        pushSet.entrySet().iterator();
    if (iter.hasNext()) {
      final Map.Entry<String, Hashtable<String, String>> entry = iter.next();
      // initialize the parsed file
      FileUtils.copyFile(new File(srcDir, "conrefpush_stub2_backup.xml"), new File(entry.getKey()));
      final Content content = new ContentImpl();
      content.setValue(entry.getValue());
      parser.setContent(content);
      parser.write(entry.getKey());
      final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      final DocumentBuilder builder = factory.newDocumentBuilder();
      final Document document = builder.parse(new File(entry.getKey()));
      final Element elem = document.getDocumentElement();
      NodeList nodeList = elem.getChildNodes();
      // according to the structure, it comes to the <li> after 2 iterations.
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < nodeList.getLength(); j++) {
          if (nodeList.item(j).getNodeType() == Node.ELEMENT_NODE) {
            nodeList = nodeList.item(j).getChildNodes();
            break;
          }
        }
      }
      Element element;
      for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
          element = (Element) node;
          if (element.getAttributes().getNamedItem("id") != null
              && element.getAttributes().getNamedItem("id").getNodeValue().equals("A")) {
            // get node of before
            node = element.getPreviousSibling();
            while (node.getNodeType() != Node.ELEMENT_NODE) {
              node = node.getPreviousSibling();
            }
            assertEquals(
                "<li class=\"- topic/li task/step \"><ph class=\"- topic/ph task/cmd \">before</ph></li>",
                nodeToString((Element) node));
          } else if (element.getAttributes().getNamedItem("id") != null
              && element.getAttributes().getNamedItem("id").getNodeValue().equals("B")) {
            // get node of after
            node = element.getNextSibling();
            while (node.getNodeType() != Node.ELEMENT_NODE) {
              node = node.getNextSibling();
            }
            assertEquals(
                "<li class=\"- topic/li task/step \"><ph class=\"- topic/ph task/cmd \">after</ph></li>",
                nodeToString((Element) node));

            // get node of replacement
            node = node.getNextSibling();
            while (node.getNodeType() != Node.ELEMENT_NODE) {
              node = node.getNextSibling();
            }
            assertEquals(
                "<li class=\"- topic/li task/step \" id=\"C\"><ph class=\"- topic/ph task/cmd \">replace</ph></li>",
                nodeToString((Element) node));
          }
        }
      }
    }
  }

  private String nodeToString(final Element elem) {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(Constants.LESS_THAN).append(elem.getNodeName());
    final NamedNodeMap namedNodeMap = elem.getAttributes();
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
      stringBuffer
          .append(Constants.STRING_BLANK)
          .append(namedNodeMap.item(i).getNodeName())
          .append(Constants.EQUAL)
          .append(Constants.QUOTATION + namedNodeMap.item(i).getNodeValue() + Constants.QUOTATION);
    }
    stringBuffer.append(Constants.GREATER_THAN);
    final NodeList nodeList = elem.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
      final Node node = nodeList.item(i);
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        // If the type of current node is ELEMENT_NODE, process it
        stringBuffer.append(nodeToString((Element) node));
      }
      if (node.getNodeType() == Node.TEXT_NODE) {
        stringBuffer.append(node.getNodeValue());
      }
    }
    stringBuffer.append("</").append(elem.getNodeName()).append(Constants.GREATER_THAN);
    return stringBuffer.toString();
  }

  @AfterClass
  public static void tearDown() throws IOException {
    TestUtils.forceDelete(tempDir);
  }
}
Exemplo n.º 7
0
 @AfterClass
 public static void tearDown() throws IOException {
   TestUtils.forceDelete(tempDir);
 }
public class DebugAndFilterModuleTest {

  final File resourceDir = TestUtils.getResourceDir(DebugAndFilterModuleTest.class);
  private File tempDir;
  private final File ditaDir = new File("src" + File.separator + "main");
  private File tmpDir;
  private File inputDir;

  @Before
  public void setUp() throws IOException, DITAOTException {
    tempDir = TestUtils.createTempDir(getClass());

    inputDir = new File(resourceDir, "input");
    final File inputMap = new File(inputDir, "maps" + File.separator + "root-map-01.ditamap");
    final File outDir = new File(tempDir, "out");
    tmpDir = new File(tempDir, "temp");
    TestUtils.copy(new File(resourceDir, "temp"), tmpDir);
    final File ditaList = new File(tmpDir, "dita.list");
    final Properties props = readProperties(ditaList);
    props.put("user.input.dir", inputDir.getAbsolutePath());
    writeProperties(props, ditaList, false);
    writeProperties(props, new File(tmpDir, "dita.xml.properties"), true);

    DITAOTFileLogger.getInstance().setLogDir(tmpDir.getAbsolutePath());
    DITAOTFileLogger.getInstance()
        .setLogFile(DebugAndFilterModuleTest.class.getSimpleName() + ".log");
    DitaURIResolverFactory.setPath(tmpDir.getAbsolutePath());

    final PipelineHashIO pipelineInput = new PipelineHashIO();
    pipelineInput.setAttribute("inputmap", inputMap.getPath());
    pipelineInput.setAttribute("basedir", inputDir.getAbsolutePath());
    pipelineInput.setAttribute("inputdir", inputDir.getPath());
    pipelineInput.setAttribute("outputdir", outDir.getPath());
    pipelineInput.setAttribute("tempDir", tmpDir.getPath());
    pipelineInput.setAttribute("ditadir", ditaDir.getAbsolutePath());
    pipelineInput.setAttribute("ditaext", ".xml");
    pipelineInput.setAttribute("indextype", "xhtml");
    pipelineInput.setAttribute("encoding", "en-US");
    pipelineInput.setAttribute("targetext", ".html");
    pipelineInput.setAttribute("validate", "false");
    pipelineInput.setAttribute("generatecopyouter", "1");
    pipelineInput.setAttribute("outercontrol", "warn");
    pipelineInput.setAttribute("onlytopicinmap", "false");
    pipelineInput.setAttribute("ditalist", new File(tmpDir, "dita.list").getPath());
    pipelineInput.setAttribute("maplinks", new File(tmpDir, "maplinks.unordered").getPath());
    pipelineInput.setAttribute(Constants.ANT_INVOKER_EXT_PARAN_SETSYSTEMID, "yes");

    final AbstractFacade facade = new PipelineFacade();
    facade.setLogger(new TestUtils.TestLogger());
    facade.execute("DebugAndFilter", pipelineInput);
  }

  @Test
  public void testGeneratedFiles() throws SAXException, IOException {
    final File[] files = {
      new File("maps", "root-map-01.ditamap"),
      new File("topics", "target-topic-a.xml"),
      new File("topics", "target-topic-c.xml"),
      new File("topics", "xreffin-topic-1.xml"),
      new File("topics", "copy-to.xml"),
    };
    final Map<File, File> copyto = new HashMap<File, File>();
    copyto.put(new File("topics", "copy-to.xml"), new File("topics", "xreffin-topic-1.xml"));
    final TestHandler handler = new TestHandler();
    final XMLReader parser = XMLReaderFactory.createXMLReader();
    parser.setContentHandler(handler);
    for (final File f : files) {
      InputStream in = null;
      try {
        in = new FileInputStream(new File(tmpDir, f.getPath()));
        handler.setSource(
            new File(inputDir, copyto.containsKey(f) ? copyto.get(f).getPath() : f.getPath()));
        parser.parse(new InputSource(in));
      } finally {
        if (in != null) {
          in.close();
        }
      }
    }
  }

  @After
  public void tearDown() throws IOException {
    TestUtils.forceDelete(tempDir);
  }

  private Properties readProperties(final File ditaList) throws IOException {
    final Properties props = new Properties();
    InputStream in = null;
    try {
      in = new FileInputStream(ditaList);
      props.load(in);
    } finally {
      if (in != null) {
        in.close();
      }
    }
    return props;
  }

  private void writeProperties(final Properties props, final File ditaList, final boolean isXML)
      throws IOException {
    OutputStream out = null;
    try {
      out = new FileOutputStream(ditaList);
      if (isXML) {
        props.storeToXML(out, null);
      } else {
        props.store(out, null);
      }
    } finally {
      if (out != null) {
        out.close();
      }
    }
  }

  private static class TestHandler implements ContentHandler {

    private File source;
    private final Map<String, Integer> counter = new HashMap<String, Integer>();
    private final Set<String> requiredProcessingInstructions = new HashSet<String>();

    void setSource(final File source) {
      this.source = source;
    }

    public void characters(final char[] arg0, final int arg1, final int arg2) throws SAXException {
      // NOOP
    }

    public void endDocument() throws SAXException {
      if (!requiredProcessingInstructions.isEmpty()) {
        for (final String pi : requiredProcessingInstructions) {
          throw new AssertionError("Processing instruction " + pi + " not defined");
        }
      }
      counter.clear();
      requiredProcessingInstructions.clear();
    }

    public void endElement(final String arg0, final String arg1, final String arg2)
        throws SAXException {
      // NOOP
    }

    public void endPrefixMapping(final String arg0) throws SAXException {
      // NOOP
    }

    public void ignorableWhitespace(final char[] arg0, final int arg1, final int arg2)
        throws SAXException {
      // NOOP
    }

    public void processingInstruction(final String arg0, final String arg1) throws SAXException {
      if (requiredProcessingInstructions.contains(arg0)) {
        requiredProcessingInstructions.remove(arg0);
      }
    }

    public void setDocumentLocator(final Locator arg0) {
      // NOOP
    }

    public void skippedEntity(final String arg0) throws SAXException {
      // NOOP
    }

    public void startDocument() throws SAXException {
      requiredProcessingInstructions.add("path2project");
      requiredProcessingInstructions.add("workdir");
      requiredProcessingInstructions.add("workdir-uri");
    }

    public void startElement(
        final String uri, final String localName, final String qName, final Attributes atts)
        throws SAXException {
      final String xtrf = atts.getValue("xtrf");
      assertNotNull(xtrf);
      assertEquals(source.getAbsolutePath(), xtrf);
      final String xtrc = atts.getValue("xtrc");
      assertNotNull(xtrc);
      Integer c = counter.get(localName);
      c = c == null ? 1 : c + 1;
      counter.put(localName, c);
      assertTrue(xtrc.startsWith(localName + ":" + c + ";"));
    }

    public void startPrefixMapping(final String arg0, final String arg1) throws SAXException {
      // NOOP
    }
  }
}
Exemplo n.º 9
0
public class TestKeyrefReader {

  private static final File resourceDir = TestUtils.getResourceDir(TestKeyrefReader.class);
  private static final File srcDir = new File(resourceDir, "src");

  @Test
  public void testKeyrefReader() throws Exception {
    final File filename = new File(srcDir, "keyrefreader.xml");

    //        final Set <String> set = new HashSet<String> ();
    //        set.add("blatview");
    //        set.add("blatfeference");
    //        set.add("blatintro");
    //        set.add("keyword");
    //        set.add("escape");
    //        set.add("top");
    //        set.add("nested");
    final KeyrefReader keyrefreader = new KeyrefReader();
    //        keyrefreader.setKeys(set);
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    final Map<String, String> exp = new HashMap<String, String>();
    exp.put(
        "blatfeference",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "blatview",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "blatintro",
        "<topicref keys='blatview blatfeference blatintro' href='blatview.dita' navtitle='blatview' locktitle='yes' class='- map/topicref '/>");
    exp.put(
        "keyword",
        "<topicref keys='keyword' class='- map/topicref '><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>keyword value</keyword></keywords></topicmeta></topicref>");
    exp.put(
        "escape",
        "<topicref keys='escape' class='- map/topicref ' navtitle='&amp;&lt;&gt;&quot;&apos;'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>&amp;&lt;&gt;&quot;&apos;</keyword></keywords></topicmeta></topicref>");
    exp.put(
        "top",
        "<topicref keys='top' class='- map/topicref ' navtitle='top'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>top keyword</keyword></keywords></topicmeta><topicref keys='nested' class='- map/topicref ' navtitle='nested'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>nested keyword</keyword></keywords></topicmeta></topicref></topicref>");
    exp.put(
        "nested",
        "<topicref keys='nested' class='- map/topicref ' navtitle='nested'><topicmeta class='- map/topicmeta '><keywords class='- topic/keywords '><keyword class='- topic/keyword '>nested keyword</keyword></keywords></topicmeta></topicref>");

    TestUtils.resetXMLUnit();
    XMLUnit.setIgnoreWhitespace(true);
    assertEquals(exp.keySet(), act.keySet());
    for (Map.Entry<String, String> e : exp.entrySet()) {
      final Document ev = keyDefToDoc(e.getValue());
      final Document av = act.get(e.getKey()).element.getOwnerDocument();
      assertXMLEqual(ev, av);
    }
  }

  @Test
  public void testMergeMap() throws Exception {
    final File filename = new File(srcDir, "merged.xml");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    final Map<String, String> exp = new HashMap<String, String>();
    exp.put(
        "toner-specs",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-specs\" href=\"toner-type-a-specs.dita\"/>");
    exp.put(
        "toner-handling",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-handling\" href=\"toner-type-b-handling.dita\"/>");
    exp.put(
        "toner-disposal",
        "<keydef class=\"+ map/topicref mapgropup-d/keydef \" keys=\"toner-disposal\" href=\"toner-type-c-disposal.dita\"/>");

    TestUtils.resetXMLUnit();
    XMLUnit.setIgnoreWhitespace(true);
    assertEquals(exp.keySet(), act.keySet());
    for (Map.Entry<String, String> e : exp.entrySet()) {
      final Document ev = keyDefToDoc(e.getValue());
      final Document av = act.get(e.getKey()).element.getOwnerDocument();
      assertXMLEqual(ev, av);
    }
  }

  private static Document keyDefToDoc(final String key) throws Exception {
    final InputSource inputSource = new InputSource(new StringReader(key));
    final DocumentBuilder documentBuilder = XMLUtils.getDocumentBuilder();
    return documentBuilder.parse(inputSource);
  }

  // Oberon Technologies' tests

  @Test
  public void testSimpleKeyscope() throws DITAOTException {
    final File filename = new File(srcDir, "simpleKeyscope.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    assertEquals(6, act.keySet().size());
    assertEquals("one.dita", act.get("test1").href.toString());
    assertEquals("four.dita", act.get("test2").href.toString());
    assertNull(act.get("test3"));
    assertEquals("two.dita", act.get("scope1.test2").href.toString());
    assertEquals("three.dita", act.get("scope1.test3").href.toString());
    assertEquals("two.dita", act.get("scope2.test2").href.toString());
    assertEquals("three.dita", act.get("scope2.test3").href.toString());

    final KeyScope scope1 = act.getChildScope("scope1");
    assertEquals(7, scope1.keySet().size());
    assertEquals("one.dita", scope1.get("test1").href.toString());
    assertEquals("four.dita", scope1.get("test2").href.toString());
    assertEquals("three.dita", scope1.get("test3").href.toString());
    assertEquals("two.dita", scope1.get("scope1.test2").href.toString());
    assertEquals("three.dita", scope1.get("scope1.test3").href.toString());
    assertEquals("two.dita", scope1.get("scope2.test2").href.toString());
    assertEquals("three.dita", scope1.get("scope2.test3").href.toString());

    final KeyScope scope2 = act.getChildScope("scope2");
    assertEquals(7, scope2.keySet().size());
    assertEquals("one.dita", scope1.get("test1").href.toString());
    assertEquals("four.dita", scope2.get("test2").href.toString());
    assertEquals("three.dita", scope2.get("test3").href.toString());
    assertEquals("two.dita", scope2.get("scope1.test2").href.toString());
    assertEquals("three.dita", scope2.get("scope1.test3").href.toString());
    assertEquals("two.dita", scope2.get("scope2.test2").href.toString());
    assertEquals("three.dita", scope2.get("scope2.test3").href.toString());

    //        KeySpace keyspace = loadKeySpace("simpleKeyscope/simpleKeyscope.ditamap");
    //        testKeyAttr(keyspace, "test1", "id", "one");
    //        testKeyAttr(keyspace, "test2", "id", "four");
    //        testKeyAttr(keyspace, "test3", "id", null);
    //        testKeyAttr(keyspace, "scope1.test2", "id", "two");
    //        testKeyAttr(keyspace, "scope1.test3", "id", "three");
    //        testKeyAttr(keyspace, "scope2.test2", "id", "two");
    //        testKeyAttr(keyspace, "scope2.test3", "id", "three");
    //
    //        KeySpace scope1 = keyspace.findChildScope("scope1");
    //        assertNotNull(scope1);
    //        testKeyAttr(scope1, "test1", "id", "one");
    //        testKeyAttr(scope1, "test2", "id", "four");
    //        testKeyAttr(scope1, "test3", "id", "three");
    //        testKeyAttr(scope1, "scope1.test2", "id", "two");
    //        testKeyAttr(scope1, "scope1.test3", "id", "three");
    //        testKeyAttr(scope1, "scope2.test2", "id", "two");
    //        testKeyAttr(scope1, "scope2.test3", "id", "three");
    //
    //        KeySpace scope2 = keyspace.findChildScope("scope2");
    //        assertNotNull(scope2);
    //        testKeyAttr(scope2, "test1", "id", "one");
    //        testKeyAttr(scope2, "test2", "id", "four");
    //        testKeyAttr(scope2, "test3", "id", "three");
    //        testKeyAttr(scope2, "scope1.test2", "id", "two");
    //        testKeyAttr(scope2, "scope1.test3", "id", "three");
    //        testKeyAttr(scope2, "scope2.test2", "id", "two");
    //        testKeyAttr(scope2, "scope2.test3", "id", "three");
  }

  @Test
  public void testQualifiedKeyOverride() throws DITAOTException {
    final File filename = new File(srcDir, "qualifiedKeyOverride.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    assertEquals(6, act.keySet().size());
    assertEquals("one.dita", act.get("test1").href.toString());
    assertEquals("four.dita", act.get("test2").href.toString());
    assertEquals("one.dita", act.get("scope1.test1").href.toString());
    assertEquals("four.dita", act.get("scope1.test2").href.toString());
    assertEquals("two.dita", act.get("scope2.test1").href.toString());
    assertEquals("three.dita", act.get("scope2.test2").href.toString());

    final KeyScope scope1 = act.getChildScope("scope1");
    assertEquals(6, scope1.keySet().size());
    assertEquals("one.dita", scope1.get("test1").href.toString());
    assertEquals("four.dita", scope1.get("test2").href.toString());
    assertEquals("one.dita", scope1.get("scope1.test1").href.toString());
    // FIXME: Should be three.dita
    assertEquals("four.dita", scope1.get("scope1.test2").href.toString());
    assertEquals("two.dita", scope1.get("scope2.test1").href.toString());
    assertEquals("three.dita", scope1.get("scope2.test2").href.toString());

    //        final KeyScope scope2 = act.getChildScope("scope2");
    //        assertEquals(6, scope2.keySet().size());
    //        assertEquals("one.dita", scope2.get("test1").href.toString());
    //        assertEquals("four.dita", scope2.get("test2").href.toString());
    //        assertEquals("two.dita", scope2.get("scope1.test1").href.toString());
    //        assertEquals("three.dita", scope2.get("scope1.test2").href.toString());
    //        assertEquals("two.dita", scope2.get("scope2.test1").href.toString());
    //        assertEquals("three.dita", scope2.get("scope2.test2").href.toString());

    //        KeySpace keyspace = loadKeySpace("qualifiedKeyOverride/map.ditamap");
    //        testKeyAttr(keyspace, "test1", "id", "one");
    //        testKeyAttr(keyspace, "test2", "id", "four");
    //        testKeyAttr(keyspace, "scope1.test1", "id", "one");
    //        testKeyAttr(keyspace, "scope1.test2", "id", "three");
    //        testKeyAttr(keyspace, "scope2.test1", "id", "two");
    //        testKeyAttr(keyspace, "scope2.test2", "id", "three");
    //
    //        KeySpace scope1 = keyspace.findChildScope("scope1");
    //        assertNotNull(scope1);
    //        testKeyAttr(scope1, "test1", "id", "one");
    //        testKeyAttr(scope1, "test2", "id", "four");
    //        testKeyAttr(scope1, "scope1.test1", "id", "one");
    //        testKeyAttr(scope1, "scope1.test2", "id", "three");
    //        testKeyAttr(scope1, "scope2.test1", "id", "two");
    //        testKeyAttr(scope1, "scope2.test2", "id", "three");
  }

  @Test
  public void testMapWithKeyscopes() throws DITAOTException {
    final File filename = new File(srcDir, "map-with-keyscopes.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    assertEquals(6, root.keySet().size());
    assertEquals(null, root.get("dita-europe.conferenceName").href);
    assertEquals(
        "http://www.ihg.com/holidayinn/hotels/us/en/munich/muchb/hoteldetail",
        root.get("dita-europe.hotel").href.toString());
    assertEquals("images/holidayInn.jpg", root.get("dita-europe.hotelImage").href.toString());
    assertEquals(null, root.get("telematics.conferenceName").href);
    assertEquals("http://www.dolcemunich.com/", root.get("telematics.hotel").href.toString());
    assertEquals("images/dolce.jpg", root.get("telematics.hotelImage").href.toString());

    final KeyScope first = root.getChildScope("dita-europe");
    assertEquals(9, first.keySet().size());
    assertEquals(null, first.get("conferenceName").href);
    assertEquals(
        "http://www.ihg.com/holidayinn/hotels/us/en/munich/muchb/hoteldetail",
        first.get("hotel").href.toString());
    assertEquals("images/holidayInn.jpg", first.get("hotelImage").href.toString());
    assertEquals(null, first.get("dita-europe.conferenceName").href);
    assertEquals(
        "http://www.ihg.com/holidayinn/hotels/us/en/munich/muchb/hoteldetail",
        first.get("dita-europe.hotel").href.toString());
    assertEquals("images/holidayInn.jpg", first.get("dita-europe.hotelImage").href.toString());
    assertEquals(null, first.get("telematics.conferenceName").href);
    assertEquals("http://www.dolcemunich.com/", first.get("telematics.hotel").href.toString());
    assertEquals("images/dolce.jpg", first.get("telematics.hotelImage").href.toString());

    final KeyScope second = root.getChildScope("telematics");
    assertEquals(9, second.keySet().size());
    assertEquals(null, second.get("conferenceName").href);
    assertEquals("http://www.dolcemunich.com/", second.get("hotel").href.toString());
    assertEquals("images/dolce.jpg", second.get("hotelImage").href.toString());
    assertEquals(null, second.get("dita-europe.conferenceName").href);
    assertEquals(
        "http://www.ihg.com/holidayinn/hotels/us/en/munich/muchb/hoteldetail",
        second.get("dita-europe.hotel").href.toString());
    assertEquals("images/holidayInn.jpg", second.get("dita-europe.hotelImage").href.toString());
    assertEquals(null, second.get("telematics.conferenceName").href);
    assertEquals("http://www.dolcemunich.com/", second.get("telematics.hotel").href.toString());
    assertEquals("images/dolce.jpg", second.get("telematics.hotelImage").href.toString());
  }

  @Test
  public void testMaprefKeyscope() throws DITAOTException {
    final File filename = new File(srcDir, "maprefKeyscope.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope act = keyrefreader.getKeyDefinition();

    assertEquals(9, act.keySet().size());
    assertEquals("one.dita", act.get("scope1.key1").href.toString());
    assertEquals("two.dita", act.get("scope1.key2").href.toString());
    assertEquals("nested-one.dita", act.get("scope1.mapref.key1").href.toString());
    assertEquals("nested-three.dita", act.get("scope1.mapref.key3").href.toString());
    assertEquals("nested-one.dita", act.get("scope1.map.key1").href.toString());
    assertEquals("nested-three.dita", act.get("scope1.map.key3").href.toString());

    final KeyScope scope1 = act.getChildScope("scope1");
    assertEquals(11, scope1.keySet().size());
    assertEquals("one.dita", scope1.get("key1").href.toString());
    assertEquals("two.dita", scope1.get("key2").href.toString());
    assertEquals("one.dita", scope1.get("scope1.key1").href.toString());
    assertEquals("two.dita", scope1.get("scope1.key2").href.toString());
    assertEquals("nested-one.dita", scope1.get("scope1.mapref.key1").href.toString());
    assertEquals("nested-three.dita", scope1.get("scope1.mapref.key3").href.toString());
    assertEquals("nested-one.dita", scope1.get("scope1.map.key1").href.toString());
    assertEquals("nested-three.dita", scope1.get("scope1.map.key3").href.toString());

    final KeyScope scope3 = scope1.getChildScope("mapref");
    assertEquals(12, scope3.keySet().size());
    assertEquals("one.dita", scope3.get("key1").href.toString());
    assertEquals("two.dita", scope3.get("key2").href.toString());
    assertEquals("nested-three.dita", scope3.get("key3").href.toString());
    assertEquals("one.dita", scope3.get("scope1.key1").href.toString());
    assertEquals("two.dita", scope3.get("scope1.key2").href.toString());
    assertEquals("nested-one.dita", scope3.get("scope1.mapref.key1").href.toString());
    assertEquals("nested-three.dita", scope3.get("scope1.mapref.key3").href.toString());
    assertEquals("nested-one.dita", scope3.get("scope1.map.key1").href.toString());
    assertEquals("nested-three.dita", scope3.get("scope1.map.key3").href.toString());

    final KeyScope scope4 = scope1.getChildScope("map");
    assertEquals(12, scope4.keySet().size());
    assertEquals("one.dita", scope4.get("key1").href.toString());
    assertEquals("two.dita", scope4.get("key2").href.toString());
    assertEquals("nested-three.dita", scope4.get("key3").href.toString());
    assertEquals("one.dita", scope4.get("scope1.key1").href.toString());
    assertEquals("two.dita", scope4.get("scope1.key2").href.toString());
    assertEquals("nested-one.dita", scope4.get("scope1.mapref.key1").href.toString());
    assertEquals("nested-three.dita", scope4.get("scope1.mapref.key3").href.toString());
    assertEquals("nested-one.dita", scope4.get("scope1.map.key1").href.toString());
    assertEquals("nested-three.dita", scope4.get("scope1.map.key3").href.toString());

    final KeyScope scope2 = act.getChildScope("scope2");
    assertEquals(12, scope2.keySet().size());
    assertEquals("one.dita", scope2.get("key1").href.toString());
    assertEquals("two.dita", scope2.get("key2").href.toString());
    assertEquals("nested-three.dita", scope2.get("key3").href.toString());
    assertEquals("one.dita", scope2.get("scope1.key1").href.toString());
    assertEquals("two.dita", scope2.get("scope1.key2").href.toString());
    assertEquals("nested-one.dita", scope2.get("scope1.mapref.key1").href.toString());
    assertEquals("nested-three.dita", scope2.get("scope1.mapref.key3").href.toString());
    assertEquals("nested-one.dita", scope2.get("scope1.map.key1").href.toString());
    assertEquals("nested-three.dita", scope2.get("scope1.map.key3").href.toString());
  }

  // DITA 1.3 specificiation examples

  @Test
  public void testExample7() throws DITAOTException {
    final File filename = new File(srcDir, "example7.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    assertEquals(2, root.keySet().size());
    assertNull(root.get("key-1"));
    assertEquals("topic-1.dita", root.get("scope-1.key-1").href.toString());
    assertEquals("topic-2.dita", root.get("scope-2.key-1").href.toString());

    final KeyScope scope1 = root.getChildScope("scope-1");
    assertEquals(3, scope1.keySet().size());
    assertEquals("topic-1.dita", scope1.get("key-1").href.toString());
    assertEquals("topic-1.dita", scope1.get("scope-1.key-1").href.toString());
    assertEquals("topic-2.dita", scope1.get("scope-2.key-1").href.toString());

    final KeyScope scope2 = root.getChildScope("scope-2");
    assertEquals(3, scope2.keySet().size());
    assertEquals("topic-2.dita", scope2.get("key-1").href.toString());
    assertEquals("topic-1.dita", scope2.get("scope-1.key-1").href.toString());
    assertEquals("topic-2.dita", scope2.get("scope-2.key-1").href.toString());
  }

  @Test
  public void testExample8() throws DITAOTException {
    final File filename = new File(srcDir, "example8.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    //        log(root, "");

    final KeyScope a2 = root.getChildScope("A").getChildScope("A-2");
    assertEquals(10, a2.keySet().size());
    assertEquals("a1", a2.get("a").element.getAttribute("id"));
    assertEquals("d", a2.get("d").element.getAttribute("id"));
    // FIXME
    //        assertEquals("d", a2.get("A-2.d").element.getAttribute("id"));
    assertNull(a2.get("c"));
    // FIXME
    //        assertEquals("c", a2.get("A-1.c").element.getAttribute("id"));
    assertEquals("c", a2.get("A.A-1.c").element.getAttribute("id"));

    final KeyScope b = root.getChildScope("B");
    assertEquals(9, b.keySet().size());
    assertEquals("e", b.get("e").element.getAttribute("id"));
    assertEquals("a1", b.get("a").element.getAttribute("id"));
    assertEquals("a2", b.get("B.a").element.getAttribute("id"));
    assertNull(b.get("g"));
    // FIXME
    //        assertEquals("g", b.get("B-2.g").element.getAttribute("id"));

  }

  @Test
  public void testKeysAndScope() throws DITAOTException {
    final File filename = new File(srcDir, "keysAndScope.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    assertEquals(2, root.keySet().size());

    final KeyScope a2 = root.getChildScope("potatoes");
    assertEquals(3, a2.keySet().size());
    assertEquals("potatoes.dita", a2.get("potatoes.vegetable").href.toString());
    assertEquals("potatoes.dita", a2.get("vegetable").href.toString());
    assertEquals("carrots.dita", a2.get("carrots.vegetable").href.toString());

    final KeyScope b = root.getChildScope("carrots");
    assertEquals(3, b.keySet().size());
    assertEquals("potatoes.dita", b.get("potatoes.vegetable").href.toString());
    assertEquals("carrots.dita", b.get("vegetable").href.toString());
    assertEquals("carrots.dita", b.get("carrots.vegetable").href.toString());
  }

  @Test
  public void testMultipleValues() throws DITAOTException {
    final File filename = new File(srcDir, "multipleValues.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    assertEquals(4, root.keySet().size());

    final KeyScope c1 = root.getChildScope("potatoes");
    assertEquals(5, c1.keySet().size());

    final KeyScope c2 = root.getChildScope("patate");
    assertEquals(5, c2.keySet().size());

    final KeyScope c3 = root.getChildScope("carrot");
    assertEquals(5, c3.keySet().size());

    final KeyScope c4 = root.getChildScope("morcov");
    assertEquals(5, c4.keySet().size());
  }

  @Test
  public void testSingleCircular() throws DITAOTException {
    final File filename = new File(srcDir, "circularSingle.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    final CachingLogger logger = new CachingLogger();
    keyrefreader.setLogger(logger);

    keyrefreader.read(filename.toURI(), readMap(filename));

    assertEquals(1, logger.getMessages().size());
    assertEquals(
        "[DOTJ069E][ERROR] Circular key definition same -> same.",
        logger.getMessages().get(0).message);
  }

  @Test
  public void testCircular() throws DITAOTException {
    final File filename = new File(srcDir, "circular.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    final CachingLogger logger = new CachingLogger();
    keyrefreader.setLogger(logger);

    keyrefreader.read(filename.toURI(), readMap(filename));

    assertEquals(3, logger.getMessages().size());
    final Set<String> act = new HashSet<>(3);
    for (final Message msg : logger.getMessages()) {
      act.add(msg.message);
    }
    assertEquals(
        new HashSet<>(
            Arrays.asList(
                "[DOTJ069E][ERROR] Circular key definition first -> second -> third -> first.",
                "[DOTJ069E][ERROR] Circular key definition second -> third -> first -> second.",
                "[DOTJ069E][ERROR] Circular key definition third -> first -> second -> third.")),
        act);
  }

  @Test
  public void testRootScope() throws DITAOTException {
    final File filename = new File(srcDir, "rootScope.ditamap");

    final KeyrefReader keyrefreader = new KeyrefReader();
    keyrefreader.read(filename.toURI(), readMap(filename));
    final KeyScope root = keyrefreader.getKeyDefinition();

    assertEquals(2, root.keySet().size());
    assertEquals("one.dita", root.get("root.test1").href.toString());
    assertEquals("two.dita", root.get("root.nested.test2").href.toString());

    final KeyScope r = root.getChildScope("root");
    assertEquals(3, r.keySet().size());
    assertEquals("one.dita", r.get("test1").href.toString());
    assertEquals("one.dita", r.get("root.test1").href.toString());
    assertEquals("two.dita", r.get("root.nested.test2").href.toString());

    final KeyScope n = r.getChildScope("nested");
    assertEquals(4, n.keySet().size());
    assertEquals("two.dita", n.get("test2").href.toString());
    assertEquals("one.dita", n.get("test1").href.toString());
    assertEquals("one.dita", n.get("root.test1").href.toString());
    assertEquals("two.dita", n.get("root.nested.test2").href.toString());
  }

  private void log(final KeyScope scope, final String indent) {
    System.err.println(indent + "scope: " + scope.name);
    for (final Map.Entry<String, KeyDef> key : scope.keyDefinition.entrySet()) {
      System.err.println(indent + " * " + key.getKey() + "=" + key.getValue().href);
    }
    for (final KeyScope child : scope.childScopes.values()) {
      log(child, indent + "  ");
    }
  }

  private Document readMap(final File file) throws DITAOTException {
    InputSource in = null;
    try {
      in = new InputSource(file.toURI().toString());
      return XMLUtils.getDocumentBuilder().parse(in);
    } catch (final Exception e) {
      throw new DITAOTException("Failed to parse map: " + e.getMessage(), e);
    } finally {
      try {
        close(in);
      } catch (IOException e) {
        // NOOP
      }
    }
  }
}