You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by sb...@locus.apache.org on 2000/02/18 07:41:09 UTC

cvs commit: xml-xalan/src/org/apache/xalan/xslt StylesheetSpec.java Process.java XSLTEngineImpl.java XSLTProcessor.java

sboag       00/02/17 22:41:09

  Modified:    src/org/apache/xalan/xslt Process.java XSLTEngineImpl.java
                        XSLTProcessor.java
  Added:       src/org/apache/xalan/xslt StylesheetSpec.java
  Log:
  In response to SPR #SBOG4G5JCF, added getAssociatedStylesheet and getAssociatedStylesheet methods, and -media switch.  It does not handle the LINK syntax.
  
  Revision  Changes    Path
  1.10      +26 -0     xml-xalan/src/org/apache/xalan/xslt/Process.java
  
  Index: Process.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/Process.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Process.java	2000/01/05 23:05:32	1.9
  +++ Process.java	2000/02/18 06:41:08	1.10
  @@ -110,6 +110,7 @@
       System.out.println(resbundle.getString("optionTEXT")); //"   [-TEXT (Use simple Text formatter.)]");
       System.out.println(resbundle.getString("optionHTML")); //"   [-HTML (Use HTML formatter.)]");
       System.out.println(resbundle.getString("optionPARAM")); //"   [-PARAM name expression (Set a stylesheet parameter)]");
  +    System.out.println("[-MEDIA use media attribute to find stylesheet associated with a document.]"); //"   [-PARAM name expression (Set a stylesheet parameter)]");
       System.out.println("[-SX (User Xerces Serializers]"); //"   [-PARAM name expression (Set a stylesheet parameter)]");
     }
     
  @@ -191,6 +192,7 @@
         FileOutputStream compiledStylesheetOutputStream = null;
         ObjectOutputStream compiledStylesheetOutput = null;     
         String outputType = null;
  +      String media = null;
         
         for (int i = 0;  i < argv.length;  i ++) 
         {
  @@ -272,6 +274,15 @@
               System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, new Object[] {"-IN"})); //"Missing argument for);
                
           }
  +        else if ("-MEDIA".equalsIgnoreCase(argv[i])) 
  +        {
  +          if ( i+1 < argv.length)
  +            media = argv[++i];
  +          else
  +            System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, new Object[] {"-MEDIA"})); //"Missing argument for);
  +             
  +        }
  +        
           else if ("-OUT".equalsIgnoreCase(argv[i])) 
           {
             if ( i+1 < argv.length)
  @@ -468,6 +479,21 @@
             {
               Node sourceTree = processor.getSourceTreeFromInput(new XSLTInputSource(inFileName));
               stylesheet.process(processor, sourceTree, new XSLTResultTarget(outputStream));
  +          }
  +          else if(null != media)
  +          {
  +            StylesheetSpec spec = processor.getAssociatedStylesheet(new XSLTInputSource("foo.xml"),
  +                                                                    media, null);
  +            if(null != spec)
  +            {
  +              processor.process(new XSLTInputSource(inFileName), 
  +                                spec, 
  +                                new XSLTResultTarget(outputStream));
  +            }
  +            else
  +            {
  +              throw new XSLProcessorException("No stylesheet found for media: "+media);
  +            }
             }
             else
             {
  
  
  
  1.40      +139 -2    xml-xalan/src/org/apache/xalan/xslt/XSLTEngineImpl.java
  
  Index: XSLTEngineImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/XSLTEngineImpl.java,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- XSLTEngineImpl.java	2000/02/18 04:17:45	1.39
  +++ XSLTEngineImpl.java	2000/02/18 06:41:08	1.40
  @@ -873,6 +873,144 @@
       }
       return doc;
     }
  +  
  +  /**
  +   * Get the preferred InputSource specification that is associated with a 
  +   * given document specified in the source param,
  +   * via the xml-stylesheet processing instruction 
  +   * (see http://www.w3.org/TR/xml-stylesheet/), and that matches 
  +   * the given criteria.   Does not yet handle the LINK REL="stylesheet" syntax.
  +   * @param media The media attribute to be matched.  May be null, in which 
  +   *              case the prefered stylesheet will be used (i.e. alternate = no).
  +   * @param title The value of the title attribute to match.  May be null.
  +   * @param charset The value of the charset attribute to match.  May be null.
  +   * @returns An InputSource that can be passed to createStylesheet method.
  +   */
  +  public StylesheetSpec getAssociatedStylesheet(XSLTInputSource source,
  +                                                 String media, 
  +                                                 String charset)
  +    throws SAXException
  +  {
  +    Vector sources = getAssociatedStylesheets(source, media, charset);
  +    if(null != sources)
  +      return (StylesheetSpec)sources.elementAt(0);
  +    else
  +      return null;
  +  }
  +
  +  
  +  /**
  +   * Get the InputSource specifications that are associated with a 
  +   * given document specified in the source param,
  +   * via the xml-stylesheet processing instruction 
  +   * (see http://www.w3.org/TR/xml-stylesheet/), and that matches 
  +   * the given criteria.   Does not yet handle the LINK REL="stylesheet" syntax.
  +   * @param media The media attribute to be matched.  May be null, in which 
  +   *              case the prefered stylesheet will be used (i.e. alternate = no).
  +   * @param title The value of the title attribute to match.  May be null.
  +   * @param charset The value of the charset attribute to match.  May be null.
  +   * @returns A list of input sources that can be passed to createStylesheet method.
  +   */
  +  public Vector getAssociatedStylesheets(XSLTInputSource source,
  +                                         String mediaRequest, 
  +                                         String charsetRequest)
  +    throws SAXException
  +  {
  +    Vector inputSources = null;
  +    Node sourceTree = getSourceTreeFromInput(source);
  +    
  +    for(Node child=sourceTree.getFirstChild(); null != child; child=child.getNextSibling())
  +    {
  +      if(Node.PROCESSING_INSTRUCTION_NODE == child.getNodeType())
  +      {
  +        ProcessingInstruction pi = (ProcessingInstruction)child;
  +        if(pi.getNodeName().equals("xml-stylesheet")
  +           || pi.getNodeName().equals("xml:stylesheet"))
  +        {
  +          String href = null; // CDATA #REQUIRED
  +          String type = null; // CDATA #REQUIRED
  +          String title = null; // CDATA #IMPLIED
  +          String media = null; // CDATA #IMPLIED
  +          String charset = null; // CDATA #IMPLIED
  +          boolean alternate = false; // (yes|no) "no"
  +
  +          StringTokenizer tokenizer = new StringTokenizer(pi.getNodeValue(), " \t=");
  +          while(tokenizer.hasMoreTokens())
  +          {
  +            String name = tokenizer.nextToken();
  +            if(name.equals("type"))
  +            {
  +              String typeVal = tokenizer.nextToken();
  +              type = typeVal.substring(1, typeVal.length()-1);                
  +            }
  +            else if(name.equals("href"))
  +            {
  +              href = tokenizer.nextToken();
  +              href = href.substring(1, href.length()-1);
  +            }
  +            else if(name.equals("title"))
  +            {
  +              title = tokenizer.nextToken();
  +              title = title.substring(1, title.length()-1);
  +            }
  +            else if(name.equals("media"))
  +            {
  +              media = tokenizer.nextToken();
  +              media = media.substring(1, media.length()-1);
  +            }
  +            else if(name.equals("charset"))
  +            {
  +              charset = tokenizer.nextToken();
  +              charset = charset.substring(1, charset.length()-1);
  +            }
  +            else if(name.equals("alternate"))
  +            {
  +              String alternateStr = tokenizer.nextToken();
  +              alternate = alternateStr.substring(1, alternateStr.length()-1).equals("yes");
  +            }
  +          }
  +          
  +          if((null != type) && type.equals("text/xsl") && (null != href))
  +          {
  +            
  +            if(null != mediaRequest)
  +            {
  +              if(null != media)
  +              {
  +                if(!media.equals(mediaRequest))
  +                  continue;
  +              }
  +              else
  +                continue;
  +            }
  +            if(null != charsetRequest)
  +            {
  +              if(null != charset)
  +              {
  +                if(!charset.equals(charsetRequest))
  +                  continue;
  +              }
  +              else
  +                continue;
  +            }
  +            if(null == inputSources)
  +              inputSources = new Vector();
  +            
  +            StylesheetSpec spec 
  +              = new StylesheetSpec(href, type, title, media, alternate, charset);
  +            
  +            if(!alternate)
  +              inputSources.insertElementAt(spec, 0);
  +            else
  +              inputSources.addElement(spec);
  +          }
  +        }
  +        
  +      }
  +    } // end for(int i = 0; i < nNodes; i++)
  +    
  +    return inputSources;
  +  }
   
     /**
      * Reset the state of the XSL processor by reading in a new
  @@ -885,8 +1023,7 @@
       throws SAXException,
       MalformedURLException,
       FileNotFoundException,
  -    IOException,
  -    SAXException
  +    IOException
     {
       Stylesheet stylesheet = null;
       String[] stringHolder =
  
  
  
  1.7       +36 -0     xml-xalan/src/org/apache/xalan/xslt/XSLTProcessor.java
  
  Index: XSLTProcessor.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/src/org/apache/xalan/xslt/XSLTProcessor.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- XSLTProcessor.java	1999/12/16 06:10:45	1.6
  +++ XSLTProcessor.java	2000/02/18 06:41:09	1.7
  @@ -57,6 +57,7 @@
   package org.apache.xalan.xslt;
   
   import java.util.TooManyListenersException;
  +import java.util.Vector;
   
   import org.apache.xalan.xpath.XObject;
   import org.apache.xalan.xpath.XString;
  @@ -170,6 +171,41 @@
      * Get the XML Parser Liaison that this processor uses.
      */
     public XMLParserLiaison getXMLProcessorLiaison();
  +  
  +  /**
  +   * Get the preferred StylesheetSpec that is associated with a 
  +   * given document specified in the source param,
  +   * via the xml-stylesheet processing instruction 
  +   * (see http://www.w3.org/TR/xml-stylesheet/), and that matches 
  +   * the given criteria.  Does not yet handle the LINK REL="stylesheet" syntax.
  +   * @param media The media attribute to be matched.  May be null, in which 
  +   *              case the prefered stylesheet will be used (i.e. alternate = no).
  +   * @param title The value of the title attribute to match.  May be null.
  +   * @param charset The value of the charset attribute to match.  May be null.
  +   * @returns An InputSource that can be passed to createStylesheet method.
  +   */
  +  public StylesheetSpec getAssociatedStylesheet(XSLTInputSource source,
  +                                                      String media, 
  +                                                      String charset)
  +    throws SAXException;
  +  
  +  /**
  +   * Get a list of StylesheetSpec specifications that are associated with a 
  +   * given document specified in the source param,
  +   * via the xml-stylesheet processing instruction 
  +   * (see http://www.w3.org/TR/xml-stylesheet/), and that matches 
  +   * the given criteria.   Does not yet handle the LINK REL="stylesheet" syntax.
  +   * @param media The media attribute to be matched.  May be null, in which 
  +   *              case the prefered stylesheet will be used (i.e. alternate = no).
  +   * @param title The value of the title attribute to match.  May be null.
  +   * @param charset The value of the charset attribute to match.  May be null.
  +   * @returns A list of InputSources that can be passed to createStylesheet method.
  +   */
  +  public Vector getAssociatedStylesheets(XSLTInputSource source,
  +                                                      String media, 
  +                                                      String charset)
  +    throws SAXException;
  +
     
     /**
      * Convenience function to create an XString.
  
  
  
  1.1                  xml-xalan/src/org/apache/xalan/xslt/StylesheetSpec.java
  
  Index: StylesheetSpec.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "XSLT4J" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, Lotus
   * Development Corporation., http://www.lotus.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.xalan.xslt;
  
  public class StylesheetSpec extends XSLTInputSource
  {
    String type; // CDATA #REQUIRED
    String title; // CDATA #IMPLIED
    String media; // CDATA #IMPLIED
    boolean alternate; // (yes|no) "no"
    
    public StylesheetSpec(String href, String type, String title, 
                          String media, boolean alternate,
                          String encoding)
    {
      this.setSystemId(href);
      this.setEncoding(encoding);
      this.type = type;
      this.title = title;
      this.media = media;
      this.alternate = alternate;
    }
  
  public String       getType()
  {
      return type;
  }
  
    public String       getTitle()
  {
      return title;
  }
  
    public String       getMedia()
  {
      return media;
  }

  public boolean    getIsAlternate()
  {
      return alternate;
  }
  }