]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
speedup of build process: added a compiled stylesheet cache and a merge capability...
authorfotis <fotis@unknown>
Sun, 12 Nov 2000 20:11:47 +0000 (20:11 +0000)
committerfotis <fotis@unknown>
Sun, 12 Nov 2000 20:11:47 +0000 (20:11 +0000)
I added the use of <xsl:key> to the font-file.xsl (contributed by Kelly Campbell)

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@193789 13f79535-47bb-0310-9956-ffa450edef68

build.xml
lib/Xslt.class
lib/Xslt.java
src/codegen/font-file.xsl

index fa4dd1fe42c0b8cebb8563f13593036fafc76636..777aa1fb1ba8c512f3d50fd4a0ef52c42b06d587 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -303,33 +303,33 @@ Sometimes ant gives out this warnings, but the build is finished without any pro
         outfile="${build.src}/${replacestring}/fo/properties/svgenums_${ignore_this}" smart="yes"/>
     <xslt infile="${charlist.xml}" xsltfile="${charlist.xsl}" 
         outfile="${build.src}/${replacestring}/render/pdf/CodePointMapping.java" smart="yes"/>
-    <xslt infile="${Courier.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Courier.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/Courier.java" smart="yes"/>
-    <xslt infile="${Courier-Oblique.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Courier-Oblique.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/CourierOblique.java" smart="yes"/>
-    <xslt infile="${Courier-Bold.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Courier-Bold.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/CourierBold.java" smart="yes"/>
-    <xslt infile="${Courier-BoldOblique.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Courier-BoldOblique.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/CourierBoldOblique.java" smart="yes"/>
-    <xslt infile="${Helvetica.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Helvetica.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/Helvetica.java" smart="yes"/>
-    <xslt infile="${Helvetica-Oblique.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Helvetica-Oblique.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/HelveticaOblique.java" smart="yes"/>
-    <xslt infile="${Helvetica-Bold.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Helvetica-Bold.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/HelveticaBold.java" smart="yes"/>
-    <xslt infile="${Helvetica-BoldOblique.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Helvetica-BoldOblique.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/HelveticaBoldOblique.java" smart="yes"/>
-    <xslt infile="${Times-Roman.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Times-Roman.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/TimesRoman.java" smart="yes"/>
-    <xslt infile="${Times-Italic.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Times-Italic.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/TimesItalic.java" smart="yes"/>
-    <xslt infile="${Times-Bold.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Times-Bold.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/TimesBold.java" smart="yes"/>
-    <xslt infile="${Times-BoldItalic.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Times-BoldItalic.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/TimesBoldItalic.java" smart="yes"/>
-    <xslt infile="${ZapfDingbats.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${ZapfDingbats.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/ZapfDingbats.java" smart="yes"/>
-    <xslt infile="${Symbol.xml}" xsltfile="${fontfile.xsl}"
+    <xslt infile="${Symbol.xml}" xsltfile="${fontfile.xsl}" mergefile="${charlist.xml}"
         outfile="${build.src}/${replacestring}/render/pdf/fonts/Symbol.java" smart="yes"/>
 
     <!-- custom fonts (Use t1fontfile.xsl instead of fontfile.xsl for Type 1 fonts!) step 2/2 -->
index 63a56aa9ffb25065e236ec989699cf13e9a2a0d7..e1ce75e48c02c6748aa6c75de538791f3cf4d946 100644 (file)
Binary files a/lib/Xslt.class and b/lib/Xslt.class differ
index b2daa1ee8596d2fed1b2c368da1a7734ef05c2e3..af50d9b27fbff582f67811b478bc176c7d056810 100644 (file)
@@ -59,6 +59,10 @@ import java.net.*;
 import java.io.*;
 import java.util.*;
 import org.apache.xalan.xslt.*;
+import org.w3c.dom.*;
+import org.xml.sax.SAXException;
+import org.w3c.dom.*;
+import org.xml.sax.SAXException;
 
 /**
  * Task to call the XSLT processor Xalan (part of xml.apache.org), which converts xml files  
@@ -76,21 +80,25 @@ import org.apache.xalan.xslt.*;
  * <p>  
  * Of these arguments, <b>infile, outfile</b> and <b>xsltfile</b> are required.
  * <p>smart defaults to 'no'. The other allowed value is 'yes'. If smart is set to 'yes'
+ * <P>
  * xalan is only called if either the outfile is older than the infile or the stylesheet
  * or the outfile doesn't exist. 
+ * <P>
  * <p>dependent defaults to 'none'. Other possible values: a comma delimited list of file names
  * which date is checked against the output file. This way you can name files which, if 
  * they have been modified, initiate a restart of the xslt process, like external entities etc.
  * <p>
  * @author Fotis Jannidis <a href="mailto:fotis@jannidis.de">fotis@jannidis.de</a>
+ * @author Kelly A. Campbell <a href="mailto:camk@camk.net">camk@camk.net</a>
  */
 
 
 public class Xslt extends Task {
-  private String infile, outfile, xsltfile;
+  private String infile, outfile, xsltfile, mergefile;
   private String smart = "no";  //defaults to do conversion everytime task is called
   private String dependent = "none"; //defaults to no dependencies
   private boolean startXslt = false; 
+    
 
 
   /**
@@ -101,6 +109,11 @@ public class Xslt extends Task {
     this.infile = infile;
   }
 
+    public void setMergefile (String mergefile) 
+    {
+  this.mergefile = mergefile;
+    }
+    
   /**
    * Sets the stylesheet file
    *
@@ -144,6 +157,37 @@ public class Xslt extends Task {
     this.dependent = dependent;
   }
   
+
+    /**
+     * Builds a document from the given file, merging the mergefile onto the end of the root node
+     */
+    private org.w3c.dom.Document buildDocument(String xmlFile) 
+  throws IOException, SAXException
+    {
+  try {
+      
+      javax.xml.parsers.DocumentBuilder docBuilder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
+      Document doc = docBuilder.parse(new FileInputStream(xmlFile));
+  
+      if (mergefile != null && !mergefile.equals("")) {
+      
+    File mergefileF = new File(mergefile);
+      
+    Document mergedoc = docBuilder.parse(new FileInputStream(mergefileF));
+    Node mergenode = doc.importNode(mergedoc.getDocumentElement(), true);
+    doc.getDocumentElement().appendChild(mergenode);
+      }
+
+      return doc;
+  }
+  catch (javax.xml.parsers.ParserConfigurationException e) {
+      System.out.println("Task xslt - SAX ERROR:\n      " + e.getMessage()); 
+  }
+  return null;
+  
+    }
+    
+
   /**
    * Calls Xalan and does the transformation
    *
@@ -153,15 +197,14 @@ public class Xslt extends Task {
                                   java.net.MalformedURLException, 
                                   org.xml.sax.SAXException 
   {               
-    // Use XSLTProcessor to instantiate an XSLTProcessor.
-    org.apache.xalan.xslt.XSLTProcessor processor =
-                 org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor();
-                 
+      StylesheetRoot stylesheet = StylesheetCache.getStylesheet(xsltfile);
+
+      org.w3c.dom.Document source = buildDocument(infile);
+      
+         
     // Create the 3 objects the XSLTProcessor needs to perform the transformation.
     org.apache.xalan.xslt.XSLTInputSource xmlSource = 
-                        new org.apache.xalan.xslt.XSLTInputSource (infile);
-    org.apache.xalan.xslt.XSLTInputSource xslSheet = 
-                       new org.apache.xalan.xslt.XSLTInputSource (xsltfile);
+                        new org.apache.xalan.xslt.XSLTInputSource (source);
     org.apache.xalan.xslt.XSLTResultTarget xmlResult = 
                        new org.apache.xalan.xslt.XSLTResultTarget (outfile);
 
@@ -169,7 +212,9 @@ public class Xslt extends Task {
     System.out.println("============================");
     System.out.println("xslt \nin: " + infile + "\nstyle: " + xsltfile + "\nout: " + outfile);
     System.out.println("============================");
-    processor.process(xmlSource, xslSheet, xmlResult);
+
+    stylesheet.process(xmlSource, xmlResult);
+
   } //end transform
     
   /**
@@ -284,6 +329,46 @@ public class Xslt extends Task {
       xslt.setSmart("yes");
       xslt.setDependent("test1,test2");
       xslt.execute();
-  } */
+  } */    
+  
+  /**
+     * Cache for stylesheets we've already processed 
+     */
+    protected static class StylesheetCache 
+    {
+  /** Cache of compiled stylesheets (filename, StylesheetRoot) */
+  private static Hashtable _stylesheetCache = new Hashtable();
+  
+  /**
+   * Returns a compiled StylesheetRoot object for a given filename
+   */
+  public static StylesheetRoot getStylesheet(String xsltFilename) 
+      throws org.xml.sax.SAXException
+  {
+      if (_stylesheetCache.containsKey(xsltFilename)) {
+    return (StylesheetRoot)_stylesheetCache.get(xsltFilename);
+      }
+      
+      // Use XSLTProcessor to instantiate an XSLTProcessor.
+      org.apache.xalan.xslt.XSLTProcessor processor =
+    org.apache.xalan.xslt.XSLTProcessorFactory.getProcessor(new org.apache.xalan.xpath.xdom.XercesLiaison());
+  
+
+      org.apache.xalan.xslt.XSLTInputSource xslSheet = 
+    new org.apache.xalan.xslt.XSLTInputSource (xsltFilename);
+  
+      // Perform the transformation.
+      System.out.println("****************************");
+      System.out.println("xslt compile \nin: " + xsltFilename);
+      System.out.println("****************************");
+  
+      StylesheetRoot compiledSheet = processor.processStylesheet(xslSheet);
+  
+      _stylesheetCache.put(xsltFilename, compiledSheet);
+      return compiledSheet;
+  }
+    }
+
+   
     
 }
index 3c2f5ee8f52200872ea4c59423aabf082c2f4001..902be25f67a8525d3f951d37e26165e34407823a 100644 (file)
@@ -14,6 +14,10 @@ file charlist.xml and extracts the WinAnsi code.
                 extension-element-prefixes="redirect">
 <xsl:output method="text" />
 
+<!-- note that match in xsl:key doesn't like document('charlist.xml'), so the charlist 
+     must be merged with the source xml at build time by the Xslt task -->
+<xsl:key name="adobe-char-map" match="/font-metrics/font-mappings/map" use="@adobe-name"/>
+
 <xsl:template match="font-metrics">
 <xsl:variable name="class-name" select="class-name"/>
 <!--<redirect:write select="concat('org/apache/fop/render/pdf/fonts/', $class-name, '.java')">-->
@@ -34,7 +38,7 @@ public class <xsl:value-of select="class-name"/> extends Font {
 
     static {
         width = new int[256];
-<xsl:for-each select="widths/char"><xsl:variable name="char-name" select="@name"/><xsl:variable name="char-num" select="document('charlist.xml')/font-mappings/map[@adobe-name=$char-name]/@win-ansi"/><xsl:if test="$char-num!='-1'">        width[<xsl:value-of select="$char-num"/>] = <xsl:value-of select="@width"/>;
+<xsl:for-each select="widths/char"><xsl:variable name="char-name" select="@name"/><xsl:variable name="char-num" select="key('adobe-char-map',$char-name)/@win-ansi"/><xsl:if test="$char-num!='-1'">        width[<xsl:value-of select="$char-num"/>] = <xsl:value-of select="@width"/>;
 </xsl:if></xsl:for-each>
     }