logo       


r153 - in izpack-frontend/trunk/src: . utils: msg#00030

Subject: r153 - in izpack-frontend/trunk/src: . utils
Author: gumbo
Date: 2005-09-06 08:37:01 +0200 (Tue, 06 Sep 2005)
New Revision: 153

Added:
   izpack-frontend/trunk/src/utils/XMLDocFlattener.java
   izpack-frontend/trunk/src/utils/XMLElementList.java
Modified:
   izpack-frontend/trunk/src/build.xml
   izpack-frontend/trunk/src/utils/DTDBasedOrderer.java
   izpack-frontend/trunk/src/utils/XML.java
Log:
Created the DTD-based orderer. Now produced files should validate successfully. 
Tested against random files off the internet - works.

Modified: izpack-frontend/trunk/src/build.xml
===================================================================
--- izpack-frontend/trunk/src/build.xml 2005-09-04 06:48:34 UTC (rev 152)
+++ izpack-frontend/trunk/src/build.xml 2005-09-06 06:37:01 UTC (rev 153)
@@ -4,7 +4,7 @@
 <!-- Build file for IzPack Frontend -->
 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
 
-<project name="IzFrontend" default="compile" basedir=".">
+<project name="IzFrontend" default="build" basedir=".">
        
        <!-- Define our library paths -->
        <path id="build.classpath">

Modified: izpack-frontend/trunk/src/utils/DTDBasedOrderer.java
===================================================================
--- izpack-frontend/trunk/src/utils/DTDBasedOrderer.java        2005-09-04 
06:48:34 UTC (rev 152)
+++ izpack-frontend/trunk/src/utils/DTDBasedOrderer.java        2005-09-06 
06:37:01 UTC (rev 153)
@@ -1,22 +1,13 @@
 /*
- * Created on Sep 2, 2005
- * 
- * $Id: DTDBasedOrderer.java Feb 8, 2004 izpack-frontend
- * Copyright (C) 2001-2003 IzPack Development Group
- * 
- * File : DTDBasedOrderer.java 
- * Description : TODO Add description
- * Author's email : gumbo-96fYSP9JRkAATYTw5x5z8w@xxxxxxxxxxxxxxxx
- * 
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or any later version.
- * 
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- * 
+ * Created on Sep 2, 2005 $Id: DTDBasedOrderer.java Feb 8, 2004 izpack-frontend
+ * Copyright (C) 2001-2003 IzPack Development Group File : DTDBasedOrderer.java
+ * Description : TODO Add description Author's email : 
gumbo-96fYSP9JRkAATYTw5x5z8w@xxxxxxxxxxxxxxxx
+ * This program is free software; you can redistribute it and/or modify it 
under
+ * the terms of the GNU General Public License as published by the Free 
Software
+ * Foundation; either version 2 of the License, or any later version. This
+ * program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+ * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  * You should have received a copy of the GNU General Public License along with
  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  * Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -27,73 +18,66 @@
 import java.io.FileReader;
 import java.io.IOException;
 import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Hashtable;
 import java.util.Vector;
 
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
+import javax.swing.tree.TreeNode;
 
+import org.w3c.dom.DOMError;
+import org.w3c.dom.DOMErrorHandler;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
+import utils.XML;
+import utils.XMLDocFlattener;
+import utils.XMLElementList;
+
+import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;
 import com.wutka.dtd.DTD;
 import com.wutka.dtd.DTDElement;
-import com.wutka.dtd.DTDItem;
 import com.wutka.dtd.DTDName;
 import com.wutka.dtd.DTDParser;
 import com.wutka.dtd.DTDSequence;
 
-public class DTDBasedOrderer
+public class DTDBasedOrderer implements DOMErrorHandler
 {
     public DTDBasedOrderer(String dtdFilename)
-    {
+    {        
+        dtdModel = new DefaultTreeModel(new DefaultMutableTreeNode("DTD"));
+
+        root = (DefaultMutableTreeNode) dtdModel.getRoot();
+        
         try
         {
             FileReader reader = new FileReader(dtdFilename);
-            
+
             DTDParser parser = new DTDParser(reader);
-            
-            DTD dtd = parser.parse(true);
-            
-            Vector items = dtd.items;
-            
-            dtdModel = new DefaultTreeModel(new DefaultMutableTreeNode("DTD"));
-            root = (DefaultMutableTreeNode) dtdModel.getRoot();
-            
-            for (Iterator iter = items.iterator(); iter.hasNext();)
+
+            DTD dtd = parser.parse(false);
+
+            items = dtd.items;
+
+            dtdElemTable = dtd.elements;
+
+            for (int i = 0; i < items.size(); i++)
             {
-                Object element = iter.next();
-                
-                if (element instanceof DTDElement)
+                Object obj = items.elementAt(i);
+                if (obj != null && obj instanceof DTDElement)
                 {
-                    DTDElement dtdElem = (DTDElement) element;
-                    DTDItem dtdContent = dtdElem.content;
-                    
-                    //DTDSequence gives the ordered list of elements in the DTD
-                    if (dtdContent instanceof DTDSequence)
-                    {
-                        DefaultMutableTreeNode dtdNode = new 
DefaultMutableTreeNode(dtdElem.name);
-                        dtdElements.put(dtdElem.name, dtdNode);
-                        root.add(dtdNode);
-                        
-                        DTDSequence seq = (DTDSequence) dtdContent;
-                        
-                        DTDItem dtdItems[] = seq.getItems();
-                        
-                        for (int i = 0; i < dtdItems.length; i++)
-                        {                            
-                            dtdNode.add(new DefaultMutableTreeNode(
-                                            ( (DTDName) dtdItems[i] ).value
-                                                               ));
-                        }
-                    }                        
+                    DTDElement element = (DTDElement) obj;
+
+                    addElement(root, element);
                 }
-            }            
+
+            }
+
+            TreeNode newRoot = root.getFirstChild();
+            dtdModel.setRoot(newRoot);
+            root = (DefaultMutableTreeNode) newRoot;
+
         }
         catch (FileNotFoundException e)
         {
@@ -105,72 +89,103 @@
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
+
     }
-    
+
+    private void addElement(DefaultMutableTreeNode parent, DTDElement element)
+    {
+        // Has child elements
+        items.remove(element);
+        if (element.content instanceof DTDSequence)
+        {
+            DTDSequence seq = (DTDSequence) element.content;
+
+            Vector v = seq.getItemsVec();
+
+            DefaultMutableTreeNode curNode = new DefaultMutableTreeNode(
+                            element.name);
+            parent.add(curNode);
+
+            for (Object obj : v)
+            {
+                DTDName name = (DTDName) obj;
+
+                DTDElement elem = (DTDElement) dtdElemTable.get(name.value);
+
+                if (elem == null) continue;
+
+                addElement(curNode, elem);
+            }
+        }
+        else
+        {
+            DefaultMutableTreeNode curNode = new DefaultMutableTreeNode(
+                            element.name);
+            parent.add(curNode);
+        }
+
+    }
+
     public Document orderDocument(Document doc)
     {
-        elements = getXMLElements(doc);
+        elements = XMLDocFlattener.flattenDocument(doc);
+
         Document sortedDoc = XML.createDocument();
+
+        Node xmlRoot = elements.searchInList("installation").get(0);
+
+        xmlRoot = sortedDoc.importNode(xmlRoot, false);
+        sortedDoc.appendChild(xmlRoot);
         
-        //Find the installation node, since it's the root        
-        Node root = sortedDoc.appendChild(
-                        sortedDoc.adoptNode(elements.get("installation"))
-                        );
+        addNodes(xmlRoot, root);    
         
+        sortedDoc.getDomConfig().setParameter("comments", false);
+        sortedDoc.getDomConfig().setParameter("validate", true);
+        sortedDoc.getDomConfig().setParameter("error-handler", this);        
         
-        
+        sortedDoc.normalizeDocument();
+
         return sortedDoc;
     }
-    
-    private void addNodes(String name, Node root)
+
+    private void addNodes(Node parent, DefaultMutableTreeNode insertLocation)
     {
-        DefaultMutableTreeNode dtdNode = dtdElements.get(name);
-        
-        //Find the right node to add this new node to
-        String parentName = (String) ((DefaultMutableTreeNode) 
dtdNode.getParent() ).getUserObject();
-        
-        int index = dtdNode.getParent().getIndex(dtdNode);        
-        
-        try
+        for (int i = 0; i < insertLocation.getChildCount(); i++)
         {
-            Node parent = (Node) xpath.evaluate("//" + parentName, root, 
XPathConstants.NODE);
+            DefaultMutableTreeNode location = (DefaultMutableTreeNode) 
insertLocation
+                            .getChildAt(i);
+
+            XMLElementList xmlElements = elements
+                            .searchInList((String) location.getUserObject());
             
-            
-            
+            // Unable to find an appropriate element
+            if (xmlElements.size() == 0)            
+                continue;            
+
+            for (Node node : xmlElements)
+            {                   
+                Node adopted = parent.getOwnerDocument().importNode(node, 
true);
+                parent.appendChild(adopted);
+
+                //If there are more children in the DTD we can add, do so
+                if (location.getChildCount() != 0)
+                    addNodes(node, location);
+            }
         }
-        catch (XPathExpressionException e)
-        {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }
     }
-    
-    private HashMap<String, Node> getXMLElements(Node node)
+
+    //DTD tree stuff    
+    private DefaultTreeModel dtdModel;
+    private DefaultMutableTreeNode root;
+    private Hashtable dtdElemTable;
+    private Vector items;
+
+    //XML tree/storage stuff
+    private XMLElementList elements;
+
+    public boolean handleError(DOMError error)
     {
-        HashMap<String, Node> docElements = new HashMap<String, Node>();
-        
-        NodeList nodeList = node.getChildNodes();
-        
-        if (nodeList != null)
-        {
-            if (node.getNodeType() == Node.ELEMENT_NODE)
-            {
-                docElements.put(node.getNodeName(), node.cloneNode(false));
-            }
-            
-            for (int i = 0; i < nodeList.getLength(); i++)
-            {
-                if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE)
-                    docElements.putAll(getXMLElements(nodeList.item(i)));
-            }
-        }
-        
-        return docElements;        
+        System.out.println(error.getMessage());
+        return false;
     }
-    
-    private XPath xpath = XPathFactory.newInstance().newXPath();
-    private HashMap<String, Node> elements;
-    private HashMap<String, DefaultMutableTreeNode> dtdElements;
-    private DefaultTreeModel dtdModel;    
-    private DefaultMutableTreeNode root;
 }

Modified: izpack-frontend/trunk/src/utils/XML.java
===================================================================
--- izpack-frontend/trunk/src/utils/XML.java    2005-09-04 06:48:34 UTC (rev 
152)
+++ izpack-frontend/trunk/src/utils/XML.java    2005-09-06 06:37:01 UTC (rev 
153)
@@ -130,10 +130,14 @@
             TransformerFactory tFactory = TransformerFactory.newInstance();
             Transformer xformer = tFactory.newTransformer();            
             
-            String systemValue = (new 
File(document.getDoctype().getSystemId())).getPath();
-            System.out.println(systemValue);
-            xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemValue); 
           
-            //xformer.setOutputProperty(OutputKeys.METHOD, "xml");
+            if (document.getDoctype() != null)
+            {
+                String systemValue = (new 
File(document.getDoctype().getSystemId())).getPath();
+            
+                xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, 
systemValue);
+            }
+            
+            
             xformer.setOutputProperty(OutputKeys.INDENT, "yes");
             
             // Write to a file

Added: izpack-frontend/trunk/src/utils/XMLDocFlattener.java
===================================================================
--- izpack-frontend/trunk/src/utils/XMLDocFlattener.java        2005-09-04 
06:48:34 UTC (rev 152)
+++ izpack-frontend/trunk/src/utils/XMLDocFlattener.java        2005-09-06 
06:37:01 UTC (rev 153)
@@ -0,0 +1,42 @@
+package utils;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class XMLDocFlattener {
+       public static XMLElementList flattenDocument(Document doc)
+       {
+               XMLElementList elements = new XMLElementList();
+               
+               Element root = doc.getDocumentElement();
+               
+               processSubtree(root, elements);
+               
+               return elements;
+       }
+       
+       private static void processSubtree(Node start, XMLElementList elements)
+       {
+               //Add this node to the list
+               if (start.getNodeType() == Node.ELEMENT_NODE)
+               {
+                       elements.add(start);
+                       
+                       //Get the child nodes
+                       NodeList children = start.getChildNodes();
+                       
+                       //If there are children, process them
+                       if (children != null)
+                       {
+                               for (int childIndex = 0; childIndex < 
children.getLength(); childIndex++)
+                               {
+                                       Node child = children.item(childIndex);
+                                       
+                                       processSubtree(child, elements);
+                               }
+                       }
+               }        
+       }
+}

Added: izpack-frontend/trunk/src/utils/XMLElementList.java
===================================================================
--- izpack-frontend/trunk/src/utils/XMLElementList.java 2005-09-04 06:48:34 UTC 
(rev 152)
+++ izpack-frontend/trunk/src/utils/XMLElementList.java 2005-09-06 06:37:01 UTC 
(rev 153)
@@ -0,0 +1,28 @@
+package utils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.w3c.dom.Node;
+
+public class XMLElementList extends ArrayList<Node> {
+       public XMLElementList(int i) {
+               super(i);
+       }
+
+       public XMLElementList() {
+               super();
+       }
+
+       public XMLElementList searchInList(String name) {
+               XMLElementList matches = new XMLElementList(2);
+
+               for (Node node : this) {
+                       if (node.getNodeName().equals(name))
+                               matches.add(node);
+               }
+
+               
+               return matches;
+       }
+}


Ruby Jobs
Java Jobs
Jobs in California
more...
what
job title, keywords
where
city, state, zip
jobs by job search
Search:
Java, servers, webhosting, windows, cisco ...
more...
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
encryption.gpg....    ietf.rfc822/199...    freebsd.devel.i...    lang.haskell.li...    mail.squirrelma...    web.zope.plone....    yellowdog.gener...    text.xml.xalan....    recreation.phot...    kde.devel.educa...    hardware.bus.ca...    printing.ghosts...    voip.peering/20...    assembly/2006-0...    org.user-groups...    culture.interne...    network.i2p/200...    boot-loaders.ya...    xfree86.render/...    qnx.openqnx.dev...    jakarta.velocit...    user-groups.pal...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe