You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2008/06/21 12:27:49 UTC

svn commit: r670175 - in /myfaces/myfaces-build-tools/trunk/other: ./ normalizer/ normalizer/src/ normalizer/src/main/ normalizer/src/main/java/ normalizer/src/main/java/org/ normalizer/src/main/java/org/apache/ normalizer/src/main/java/org/apache/myfa...

Author: skitching
Date: Sat Jun 21 03:27:48 2008
New Revision: 670175

URL: http://svn.apache.org/viewvc?rev=670175&view=rev
Log:
Add simple tool to help in comparing tld or faces-config files between two different releases (or two different implementations).

Added:
    myfaces/myfaces-build-tools/trunk/other/
    myfaces/myfaces-build-tools/trunk/other/normalizer/
    myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/norm   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java   (with props)
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/
    myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java   (with props)

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt Sat Jun 21 03:27:48 2008
@@ -0,0 +1,15 @@
+This project is a simple command-line tool that "normalises" a JSP TLD file or a JSF faces-config file.
+After two files have been "normalized", they can then be compared with a simple "diff" tool to see what
+real differences (if any) there are between the two original files.
+
+All "non-critical" data, like comments and "description" fields are discarded. 
+All xml elements and attributes are then sorted into a consistent order:
+ * elements with just text content are sorted before others.
+ 
+This tool is partly a generic XML "normaliser", but takes a few shortcuts that work specifically
+on TLD and faces-config files. It could possibly be expanded to support comparison of other xml
+files, but wasn't specifically designed for that purpose. And it is definitely a "hack"; a tool
+that is just complex enough for its purpose but not elegant. For example:
+* "mixed content" elements are just not supported
+* xml namespaces are not handled properly
+* external dtds and entity references are ignored
\ No newline at end of file

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/README.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/norm
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/norm?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/norm (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/norm Sat Jun 21 03:27:48 2008
@@ -0,0 +1 @@
+java -cp target/classes org.apache.myfaces.buildtools.normalizer.Normalizer $@

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/norm
------------------------------------------------------------------------------
    svn:executable = *

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml Sat Jun 21 03:27:48 2008
@@ -0,0 +1,18 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.myfaces.buildtools</groupId>
+  <artifactId>normalizer</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+  <name>normalizer</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,30 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+import java.io.PrintWriter;
+
+public class Attribute implements Comparable
+{
+    String name;
+    String value;
+
+    public Attribute(String name, String value)
+    {
+        this.name = name;
+        this.value = value;
+    }
+
+    public void print(PrintWriter writer)
+    {
+        writer.append(name);
+        writer.append("=");
+        writer.append("\"");
+        writer.append(value);
+        writer.append("\"");
+    }
+
+    public int compareTo(Object other)
+    {
+        Attribute otherAttr = (Attribute) other;
+        return name.compareTo(otherAttr.name);
+    }    
+}
\ No newline at end of file

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Attribute.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,142 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+import java.io.PrintWriter;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Represent an XML element which has either body text or a list of child elements.
+ * <p>
+ * Mixed-content elements are not supported. Namespaces are ignored.
+ */
+public class Element implements Comparable
+{
+    String name;
+    Attribute[] attributes;
+    String content;
+    List children = new LinkedList();
+    String sortKey = null;
+
+    // The attributes array is expected to be sorted before passing in here.
+    public Element(String name, Attribute[] attributes)
+    {
+        this.name = name;
+        this.attributes = attributes;
+    }
+
+    public void setContent(String content)
+    {
+        this.content = content;
+    }
+    
+    public void addChild(Element child)
+    {
+        children.add(child);
+    }
+
+    public void sortChildren()
+    {
+        if (children.isEmpty())
+        {
+            // Nothing to sort here.
+            if (content == null)
+                sortKey = ""; // should never happen
+            else
+                sortKey = content;
+            return;
+        }
+
+        // first ensure each child is internally sorted
+        for(Iterator i = children.iterator(); i.hasNext(); )
+        {
+            ((Element) i.next()).sortChildren();
+        }
+
+        // now sort the list of children relative to each other
+        Collections.sort(children);
+        
+        // and compute the sortKey for this element
+        StringBuffer buf = new StringBuffer();
+        for(Iterator i = children.iterator(); i.hasNext(); )
+        {
+            Element child = (Element) i.next();
+            if (buf.length() == 0)
+            {
+                buf.append("&");
+            }
+            buf.append(child.sortKey);
+        }
+        sortKey = buf.toString();
+    }
+
+    public void print(PrintWriter writer, int indentAmount)
+    {
+        Utils.indent(writer, indentAmount);
+        writer.append("<");
+        writer.append(name);
+        if (attributes != null)
+        {
+            for(int i=0; i<attributes.length; ++i)
+            {
+                writer.append(" ");
+                attributes[i].print(writer);
+            }
+        }
+
+        if (content != null)
+        {
+            writer.append(">");
+            writer.append(content);
+            writer.append("</");
+            writer.append(name);
+            writer.append(">\n");
+        }
+        else if (children.size() == 0)
+        {
+            writer.append("/>\n");
+        }
+        else
+        {
+            writer.append(">\n");
+            for(Iterator i = children.iterator(); i.hasNext();)
+            {
+                Element e = (Element)i.next();
+                e.print(writer, indentAmount+1);
+            }
+            Utils.indent(writer, indentAmount);
+            writer.append("</");
+            writer.append(name);
+            writer.append(">\n");
+        }
+    }
+
+    /**
+     * Return a value that indicates how this element should be sorted relative to
+     * other elements.
+     * <p>
+     * This method is not safe to call until after sortChildren() has been invoked.
+     */
+    public int compareTo(Object other)
+    {
+        Element otherElement = (Element) other;
+
+        // Sort tags with just text content before ones with child elements
+        if (this.children.isEmpty() && !otherElement.children.isEmpty())
+            return -1;
+        else if (!this.children.isEmpty() && otherElement.children.isEmpty())
+            return +1;
+        
+        // Sort tags with different names alphabetically
+        int compare = name.compareTo(otherElement.name);
+        if (compare != 0)
+        {
+            return compare;
+        }
+
+        // Ok, we have two tags with the same name and both have child elements.
+        // Sort by the sortkey we computed during the sortChildren method
+        return sortKey.compareTo(otherElement.sortKey);
+    }
+}

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Element.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,118 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Stack;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Read input file and write it out with everything normalized.
+ */
+public class Normalizer extends DefaultHandler
+{
+    Stack elements = new Stack();
+    Element rootElement = null;
+    StringBuffer currContent = new StringBuffer();
+
+    public static void main( String[] args ) throws Exception
+    {
+        if (args.length < 1)
+        {
+            System.out.println("Usage: App inputFile");
+            System.exit(-1);
+        }
+        
+        new Normalizer().process(args[0]);
+    }
+    
+    private void process(String filename) throws Exception
+    {
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+        SAXParser parser = factory.newSAXParser();
+
+        File f = new File(filename);
+
+        parser.parse(f, this);
+        rootElement.sortChildren();
+        PrintWriter pw = new PrintWriter(System.out);
+        rootElement.print(pw, 0);
+        pw.flush();
+    }
+
+    private static final Attribute[] EMPTY_ATTRS = new Attribute[0];
+
+    // Ignore any external entities. In particular, this skips the reading of dtds.
+    public InputSource resolveEntity(String publicId, String systemId)
+     throws IOException, SAXException
+    {
+        InputStream is = new ByteArrayInputStream(new byte[0]);
+        return new InputSource(is);
+    }
+            
+    public void startElement(String uri, String localName, String qname, Attributes attributes)
+    {
+        int nAttrs = attributes.getLength();
+        List attrList = new ArrayList(nAttrs);
+        for(int i=0; i<nAttrs; ++i) {
+            String name = attributes.getQName(i);
+            String value = attributes.getValue(i);
+            Attribute a = new Attribute(name, value);
+            attrList.add(a);
+        }
+        Collections.sort(attrList);
+
+        Attribute[] attrs = (Attribute[]) attrList.toArray(EMPTY_ATTRS); 
+        Element e = new Element(qname, attrs);
+
+        Element parent;
+        if (elements.isEmpty())
+        {
+            rootElement = e;
+        }
+        else if (!qname.equals("description"))
+        {
+            parent = (Element) elements.peek();
+            parent.addChild(e);
+        }
+        elements.push(e);
+
+        // check whether currContent is already non-whitespace.
+        // if it is, then throw an exception about "mixed content not supported"
+        currContent.setLength(0);
+    }
+
+    public void endElement(String uri, String localName, String qname)
+    {
+        String s = currContent.toString().trim();
+        if (!s.isEmpty())
+        {
+            getCurrElement().setContent(s);
+        }
+        currContent.setLength(0);
+
+        elements.pop();
+    }
+
+    public void characters(char[] ch, int start, int length)
+    {
+        currContent.append(ch, start, length);
+    }
+    
+    private Element getCurrElement()
+    {
+        return (Element) elements.peek();
+    }
+}

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Normalizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,10 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+
+public class NullInputSource extends org.xml.sax.InputSource
+{
+    public NullInputSource(String pubId, String sysId)
+    {
+        
+    }
+}
\ No newline at end of file

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/NullInputSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,16 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+import java.io.PrintWriter;
+
+
+/**
+ * Read input file and write it out with everything normalized.
+ */
+public class Utils 
+{
+    public static void indent(PrintWriter writer, int amount)
+    {
+        for(int i=0; i<amount; ++i)
+            writer.write("  ");
+    }
+}

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/main/java/org/apache/myfaces/buildtools/normalizer/Utils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java
URL: http://svn.apache.org/viewvc/myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java?rev=670175&view=auto
==============================================================================
--- myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java (added)
+++ myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java Sat Jun 21 03:27:48 2008
@@ -0,0 +1,18 @@
+package org.apache.myfaces.buildtools.normalizer;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for Normalizer.
+ */
+public class NormalizerTest extends TestCase
+{
+    /**
+     * Rigorous Test :-)
+     */
+    public void testNormalizer()
+    {
+        // TODO: write some real tests here
+        assertTrue( true );
+    }
+}

Propchange: myfaces/myfaces-build-tools/trunk/other/normalizer/src/test/java/org/apache/myfaces/buildtools/normalizer/NormalizerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native