rubys       01/08/05 09:50:34

  Modified:    proposal/gump gen.java
               proposal/gump/stylesheet defaults.xsl
  Log:
  Rewrite unnesting of project logic in Java (was in XSLT)
  
  Revision  Changes    Path
  1.4       +43 -10    jakarta-alexandria/proposal/gump/gen.java
  
  Index: gen.java
  ===================================================================
  RCS file: /home/cvs/jakarta-alexandria/proposal/gump/gen.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- gen.java  2001/08/05 12:19:35     1.3
  +++ gen.java  2001/08/05 16:50:34     1.4
  @@ -21,6 +21,7 @@
   
   // Imported java classes
   import java.io.FileOutputStream;
  +import java.util.Enumeration;
   import java.util.Hashtable;
   
   public class gen {
  @@ -115,30 +116,62 @@
          }
       }
   
  -    private void collapse(String type, Hashtable list, Node parent) {
  +    /**
  +      * Merge the contents of nodes with the same value for the name attribute.
  +      * Attributes from later definitions get added (or overlay) prior
  +      * definitions.  Elements get appended.
  +      * @param type Element localname.  Typically project or repository.
  +      * @param list Hashtable used for recursion.  Must initially be empty.
  +      * @param Node Starting point for search.
  +      */
  +    private void merge(String type, Hashtable list, Node parent) {
          Node child=parent.getFirstChild();
          while (child != null) {
              Node next=child.getNextSibling();
              if (child.getNodeName().equals(type)) {
  -               Element project = (Element) child;
  -               String name = project.getAttributeNode("name").getValue();
  +               Element element = (Element) child;
  +               String name = element.getAttributeNode("name").getValue();
   
                  Element priorDefinition = (Element)list.get(name);
                  if (priorDefinition == null) {
  -                   list.put(name, project);
  +                   list.put(name, element);
                  } else {
  -                   copyChildren(project, priorDefinition);
  -                   project.getParentNode().removeChild(project);
  -                   project=priorDefinition;
  +                   copyChildren(element, priorDefinition);
  +                   element.getParentNode().removeChild(element);
  +                   element=priorDefinition;
                  }
   
  -               collapse(type, list, project);
  +               merge(type, list, element);
              }
              child=next;
          }
       }
   
       /**
  +      * Unnest all elements of a given type by moving them all to become
  +      * direct children of the specified root node.  In the process, merge
  +      * all matching nodes which contain the same value for the name attribute.
  +      * For elements that get "hoisted", an additional "defined-in" attribute
  +      * is added indicating where the element was originally defined.
  +      * @param type Element localname.  Typically project or repository.
  +      * @param Node Root (workspace) node
  +      */
  +    private void flatten(String type, Node root) {
  +        Hashtable list = new Hashtable();
  +        merge(type, list, root);
  +        for (Enumeration e=list.keys(); e.hasMoreElements();) {
  +           Element element = (Element)list.get(e.nextElement());
  +           Element parent  = (Element)element.getParentNode();
  +           if (parent != root) {
  +               parent.removeChild(element);
  +               String definedIn = parent.getAttributeNode("name").getValue();
  +               element.setAttribute("defined-in",definedIn);
  +               root.appendChild(element);
  +           }
  +        }
  +    }
  +
  +    /**
         * serialize a DOM tree to file
         * @param DOM to be transformed
         * @param destination file
  @@ -158,8 +191,8 @@
       private gen(String source) throws Exception {
           Node workspace = parse(source);
           expand((Element)workspace.getFirstChild());
  -        collapse("project", new Hashtable(), workspace.getFirstChild());
  -        collapse("repository", new Hashtable(), workspace.getFirstChild());
  +        flatten("project", workspace.getFirstChild());
  +        flatten("repository", workspace.getFirstChild());
   
           Node resolved = transform(workspace, "defaults.xsl");
           output (resolved, "work/merge.xml");
  
  
  
  1.4       +7 -45     jakarta-alexandria/proposal/gump/stylesheet/defaults.xsl
  
  Index: defaults.xsl
  ===================================================================
  RCS file: /home/cvs/jakarta-alexandria/proposal/gump/stylesheet/defaults.xsl,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- defaults.xsl      2001/08/02 02:09:32     1.3
  +++ defaults.xsl      2001/08/05 16:50:34     1.4
  @@ -67,19 +67,8 @@
           </xsl:attribute>
         </xsl:if>
   
  -      <xsl:copy-of select="@*"/>
  -
  -      <!-- pre-process each project -->
  -      <xsl:for-each select="project[not(@href)]">
  -        <xsl:call-template name="project">
  -          <xsl:with-param name="home" select="@home"/>
  -          <xsl:with-param name="basedir" select="$basedir"/>
  -          <xsl:with-param name="defined-in" select="@defined-in"/>
  -        </xsl:call-template>
  -      </xsl:for-each>
  -
         <!-- copy the rest -->
  -      <xsl:apply-templates select="*[not(self::project)]"/>
  +      <xsl:apply-templates select="* | @* | text()"/>
   
       </xsl:copy>
   
  @@ -89,12 +78,9 @@
     <!--      resolve paths and add implicit dependencies to a project       -->
     <!-- =================================================================== -->
   
  -  <xsl:template name="project">
  -    <xsl:param name="home"/>
  -    <xsl:param name="basedir"/>
  -    <xsl:param name="tag"/>
  -    <xsl:param name="defined-in"/>
  +  <xsl:template match="project">
   
  +    <xsl:variable name="basedir" select="../@basedir"/>
       <xsl:variable name="project" select="@name"/>
   
       <!-- determine the name of the source directory -->
  @@ -104,12 +90,9 @@
           <xsl:when test="@srcdir">
             <xsl:value-of select="@srcdir"/>
           </xsl:when>
  -        <xsl:when test="../@srcdir">
  -          <xsl:value-of select="../@srcdir"/>
  +        <xsl:when test="@defined-in">
  +          <xsl:value-of select="@defined-in"/>
           </xsl:when>
  -        <xsl:when test="../@name">
  -          <xsl:value-of select="../@name"/>
  -        </xsl:when>
           <xsl:otherwise>
             <xsl:value-of select="@name"/>
           </xsl:otherwise>
  @@ -119,28 +102,18 @@
       <xsl:copy>
         <xsl:apply-templates select="@*[name()!='srcdir']"/>
   
  -      <xsl:if test="$defined-in">
  -        <xsl:attribute name="defined-in">
  -          <xsl:value-of select="$defined-in"/>
  -        </xsl:attribute>
  -      </xsl:if>
  -
         <xsl:attribute name="srcdir">
           <xsl:value-of select="$srcdir"/>
         </xsl:attribute>
   
  -      <xsl:attribute name="tag">
  -        <xsl:value-of select="$tag"/>
  -      </xsl:attribute>
  -
         <xsl:apply-templates select="*[not(self::home|self::project)] | text()"/>
   
         <!-- Compute fully qualified home directory -->
   
         <home>
           <xsl:choose>
  -          <xsl:when test="$home">
  -             <xsl:value-of select="$home"/>
  +          <xsl:when test="@home">
  +             <xsl:value-of select="@home"/>
             </xsl:when>
             <xsl:when test="home/@parent">
                <xsl:value-of select="$basedir"/>
  @@ -187,17 +160,6 @@
         </xsl:for-each>
   
       </xsl:copy>
  -
  -    <!-- process nested projects -->
  -
  -    <xsl:for-each select="project">
  -      <xsl:text>&#10;&#10;</xsl:text>
  -      <xsl:call-template name="project">
  -        <xsl:with-param name="home" select="$home"/>
  -        <xsl:with-param name="basedir" select="$basedir"/>
  -        <xsl:with-param name="defined-in" select="$defined-in"/>
  -      </xsl:call-template>
  -    </xsl:for-each>
   
     </xsl:template>
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to