/* * Created on May 30, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package CDBuilder.couplingSyntaxEditor.convertToXML; import java.io.FileWriter; import java.io.Writer; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sun.org.apache.xerces.internal.impl.xs.dom.DOMParser; /** * Takes in the xml file, parses it using the DOM parser * provided with the Xerces plugin and writes the content * out into a .ma file and format. * * @author Lucie Zhao and Sherwin Sim * @version 1 May - Aug 2003 */ public class XMLReader { /** keeps track of what line the parser is at */ int counter = 0; /** makes a new file writer to generate a new .ma file */ FileWriter write= null; /** * Initializes the parser and parses the document * Makes a new .ma file with the same name as the xml file and * writes the content of the new file after parsing it * @param name the path of where the xml file is located */ public XMLReader (String name){ DOMParser parser = new DOMParser(); //makes a new parser try { parser.parse(name); //parses the document int num = name.length(); Document newDocument = parser.getDocument(); write = new FileWriter(name.substring(0, num-4) +"ma"); //a new file NodeDetails(newDocument,write); //writes the contents to the file write.close(); } catch (Exception e) { System.err.println (e); } } /** * Writes the child nodes to the new file * @param node the node to get the contents of * @param writer the file writer that has been opened */ // this is a recursive function to traverse the document tree private void PrintChild (Node node,Writer writer) { String Content = ""; int type = node.getNodeType(); //gets the node type //if the node is an element node, write in the file the node name if (type == Node.ELEMENT_NODE) { if(node.getNodeName().equals("")){ try{ writer.write(node.getNodeName()); } catch (Exception e) { System.out.println(e); } } //if the node is a text node, write the contents to file } else if (type == Node.TEXT_NODE) { // check if text node and print value Content = node.getNodeValue(); if (!Content.trim().equals("")){ try{ writer.write(Content+"\n"); } catch (Exception e) { System.out.println(e); } } //if the node is a comment node, write % and then the contents to file } else if (type == Node.COMMENT_NODE) { Content = node.getNodeValue(); if (!Content.trim().equals("")){ try{ writer.write ("%"); } catch (Exception e) { System.out.println(e); } } } } /** * Writes the nodes the parser finds to the new file * Differentiates between containers (ie. top) and keywords, comments * and just text. * @param node the node to get the contents of * @param writer the file writer that has been opened */ // this is a recursive function to traverse the document tree private void NodeDetails (Node node, Writer writer) { String Content = ""; int type = node.getNodeType(); //get node type String name = node.getNodeName(); //get the node name if (type == Node.ELEMENT_NODE) { //if the name is top, inc counter and writer [top] to file if (name.equals("top")) { counter=1; try{ writer.write ("\n["+node.getNodeName()+"]" +"\n"); } catch (Exception e) { System.out.println(e); } } else if (counter ==1&& node.hasChildNodes() && node.getParentNode().getParentNode().getNodeName().equals("CDPlusPlus_Builder_Version_1.0.0")){ try{ writer.write (node.getNodeName() +" :" ); } catch (Exception e) { System.out.println(e); } //if name is any of the keywords, write the keyword and : in the file // } else if (counter == 1){ try{ writer.write ("\n["+node.getNodeName()+"] \n"); } catch (Exception e) { System.out.println(e); } //if the first line isn't top then it's a comment node } else if(counter == 0 && !(name.equals("comment"))) { try{ writer.write ("%"+node.getNodeName()+"\n"); } catch (Exception e) { System.out.println(e); } //a comment node } else if (name.equals("comment")) { try{ writer.write ("%"); } catch (Exception e) { System.out.println(e); } } } else if (type == Node.COMMENT_NODE) { // check if comment node and print value Content = node.getNodeValue(); if (!Content.trim().equals("")){ try{ writer.write("% "); } catch (Exception e) { System.out.println(e); } } } // check if current node has any children NodeList children = node.getChildNodes(); if (children != null) { // if it does, iterate through the collection for (int i=0; i< children.getLength(); i++) { PrintChild(children.item(i),writer); //write to file all the child nodes NodeDetails(children.item(i), writer); } } } }