@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)); } } } } }