deweese     01/11/09 12:11:46

  Modified:    sources/org/apache/batik/css CSSOMStyleDeclaration.java
                        CSSOMStyleRule.java
               sources/org/apache/batik/css/value AbstractValueFactory.java
  Added:       sources/org/apache/batik/css/parser
                        ExtendedParserWrapper.java
  Log:
  1) Slightly cleaner integration of ExtendedParser.
  2) Now wraps non ExtendedParsers (slightly faster in already extended
     case, no check and dynamic cast, in not extended case the extra
     indirection is nothing compaired to the two constructors...), but
     mostly this is just a cleaner integration.
  
  Revision  Changes    Path
  1.4       +8 -23     
xml-batik/sources/org/apache/batik/css/CSSOMStyleDeclaration.java
  
  Index: CSSOMStyleDeclaration.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/css/CSSOMStyleDeclaration.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CSSOMStyleDeclaration.java        2001/11/09 17:12:02     1.3
  +++ CSSOMStyleDeclaration.java        2001/11/09 20:11:46     1.4
  @@ -16,6 +16,7 @@
   import org.apache.batik.css.event.CSSValueChangeListener;
   
   import org.apache.batik.css.parser.ExtendedParser;
  +import org.apache.batik.css.parser.ExtendedParserWrapper;
   
   import org.apache.batik.css.value.ValueFactory;
   import org.apache.batik.css.value.ValueFactoryMap;
  @@ -37,7 +38,7 @@
    * interface.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: CSSOMStyleDeclaration.java,v 1.3 2001/11/09 17:12:02 tkormann Exp $
  + * @version $Id: CSSOMStyleDeclaration.java,v 1.4 2001/11/09 20:11:46 deweese Exp $
    */
   public class CSSOMStyleDeclaration
       implements CSSStyleDeclaration,
  @@ -52,7 +53,7 @@
       /**
        * The CSS parser.
        */
  -    protected Parser parser;
  +    protected ExtendedParser parser;
   
       /**
        * The properties.
  @@ -84,18 +85,14 @@
        */
       protected CSSStyleDeclarationChangeSupport declarationChangeSupport;
   
  -    /**
  -     * Indicates whether or not the CSS parser supports methods with String.
  -     */
  -    protected boolean isExtendedParser;
   
       /**
        * Creates a new CSSStyleDeclaration object.
        */
       public CSSOMStyleDeclaration() {
        try {
  -         parser = CSSDocumentHandler.createParser();
  -         isExtendedParser = (parser instanceof ExtendedParser);
  +         Parser p = CSSDocumentHandler.createParser();
  +         parser = ExtendedParserWrapper.wrap(p);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
  @@ -105,8 +102,7 @@
        * Creates a new CSSStyleDeclaration object.
        */
       public CSSOMStyleDeclaration(CSSRule r, Parser p) {
  -     parser = p;
  -     isExtendedParser = (parser instanceof ExtendedParser);
  +     parser = ExtendedParserWrapper.wrap(p);
        parentRule = r;
       }
   
  @@ -223,12 +219,7 @@
        properties = new PropertyMap();
        try {
            parser.setDocumentHandler(handler);
  -         if (isExtendedParser) {
  -             ((ExtendedParser)parser).parseStyleDeclaration(cssText);
  -         } else {
  -             Reader r = new StringReader(cssText);
  -             parser.parseStyleDeclaration(new InputSource(r));
  -         }
  +         parser.parseStyleDeclaration(cssText);
        } catch (DOMException e) {
            properties = pm;
            oldProperties = null;
  @@ -338,13 +329,7 @@
        try {
            ValueFactory f;
               f = factories.get(propertyName.toLowerCase().intern());
  -         LexicalUnit lu;
  -         if (isExtendedParser) {
  -             lu = ((ExtendedParser)parser).parsePropertyValue(value);
  -         } else {
  -             InputSource is = new InputSource(new StringReader(value));
  -             lu = parser.parsePropertyValue(is);
  -         }
  +         LexicalUnit lu = parser.parsePropertyValue(value);
            f.createCSSValue(lu, this, prio);
        } catch (Exception e) {
               e.printStackTrace();
  
  
  
  1.5       +5 -15     xml-batik/sources/org/apache/batik/css/CSSOMStyleRule.java
  
  Index: CSSOMStyleRule.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/css/CSSOMStyleRule.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CSSOMStyleRule.java       2001/11/09 17:12:02     1.4
  +++ CSSOMStyleRule.java       2001/11/09 20:11:46     1.5
  @@ -20,6 +20,7 @@
   import org.apache.batik.css.event.CSSPropertyChangeEvent;
   
   import org.apache.batik.css.parser.ExtendedParser;
  +import org.apache.batik.css.parser.ExtendedParserWrapper;
   
   import org.apache.batik.css.value.ValueFactoryMap;
   
  @@ -41,7 +42,7 @@
    * This class implements the {@link org.w3c.dom.css.CSSStyleRule} interface.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: CSSOMStyleRule.java,v 1.4 2001/11/09 17:12:02 tkormann Exp $
  + * @version $Id: CSSOMStyleRule.java,v 1.5 2001/11/09 20:11:46 deweese Exp $
    */
   public class CSSOMStyleRule
       extends    AbstractCSSRule
  @@ -60,7 +61,7 @@
       /**
        * The CSS parser.
        */
  -    protected Parser parser;
  +    protected ExtendedParser parser;
       
       /**
        * The value factory map.
  @@ -83,11 +84,6 @@
       protected URL baseURI;
   
       /**
  -     * Indicates whether or not the CSS parser supports methods with String.
  -     */
  -    protected boolean isExtendedParser;
  -    
  -    /**
        * Creates a new rule set.
        */
       public CSSOMStyleRule(CSSStyleSheet ss,
  @@ -95,8 +91,7 @@
                          Parser p,
                          ValueFactoryMap m) {
        super(ss, pr);
  -     parser = p;
  -     isExtendedParser = (parser instanceof ExtendedParser);
  +     parser = ExtendedParserWrapper.wrap(p);
        factories = m;
        style = new CSSOMStyleDeclaration(this, p);
        style.setValueFactoryMap(m);
  @@ -151,12 +146,7 @@
            parser.setSelectorFactory(SELECTOR_FACTORY);
            parser.setConditionFactory(CONDITION_FACTORY);
            parser.setDocumentHandler(ruleHandler);
  -         if (isExtendedParser) {
  -             ((ExtendedParser)parser).parseRule(cssText);
  -         } else {
  -             InputSource is = new InputSource(new StringReader(cssText));
  -             parser.parseRule(is);
  -         }
  +         parser.parseRule(cssText);
        } catch (DOMException e) {
            style.fireCSSStyleDeclarationChangeCancel();
            fireCSSStyleRuleChangeCancel();
  
  
  
  1.1                  
xml-batik/sources/org/apache/batik/css/parser/ExtendedParserWrapper.java
  
  Index: ExtendedParserWrapper.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.css.parser;
  
  import java.io.IOException;
  import java.io.StringReader;
  import java.util.Locale;
  
  import org.w3c.css.sac.CSSException;
  import org.w3c.css.sac.LexicalUnit;
  import org.w3c.css.sac.SelectorList;
  import org.w3c.css.sac.DocumentHandler;
  import org.w3c.css.sac.SelectorFactory;
  import org.w3c.css.sac.ConditionFactory;
  import org.w3c.css.sac.ErrorHandler;
  import org.w3c.css.sac.InputSource;
  
  import org.w3c.css.sac.Parser;
  
  /**
   * This class implements the {@link org.apache.batik.css.ExtendedParser} 
   * interface by wrapping a standard {@link org.w3c.css.sac.Parser}.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Thomas DeWeese</a>
   * @version $Id: ExtendedParserWrapper.java,v 1.1 2001/11/09 20:11:46 deweese Exp $
   */
  public class ExtendedParserWrapper implements ExtendedParser {
  
      /**
       * This converts a standard @link org.w3c.css.sac.Parser into
       * an Exteneded Parser.  If it is already an ExtendedParser
       * it will simply cast it and return, otherwise it will wrap it
       * and return the result.
       * @param p Parser to wrap.
       * @return p as an ExtenedParser.
       */
      public static ExtendedParser wrap(Parser p) {
        if (p instanceof ExtendedParser)
            return (ExtendedParser)p;
  
        return new ExtendedParserWrapper(p);
      }
  
  
      public Parser parser;
  
      public ExtendedParserWrapper(Parser parser) {
        this.parser = parser;
      }
      
      /**
       * Returns a string about which CSS language is supported by this
       * parser. For CSS Level 1, it returns "CSS1", for CSS Level 2, it returns
       * "CSS2". For CSS Level 3, it returns "CSS3", etc. Note that a "CSSx"
       * parser can return lexical unit other than those allowed by CSS Level x
       * but this usage is not recommended.
       */
      public String getParserVersion() {
        return parser.getParserVersion();
      }
      
      /**
       * Allow an application to request a locale for errors and warnings.
       *
       * <p>CSS parsers are not required to provide localisation for errors
       * and warnings; if they cannot support the requested locale,
       * however, they must throw a CSS exception.  Applications may
       * not request a locale change in the middle of a parse.</p>
       *
       * @param locale A Java Locale object.
       * @exception CSSException Throws an exception
       *            (using the previous or default locale) if the 
       *            requested locale is not supported.
       * @see CSSException
       * @see CSSParseException
       */
      public void setLocale(Locale locale) throws CSSException {
        parser.setLocale(locale);
      }
  
      /**
       * Allow an application to register a document event handler.
       *
       * <p>If the application does not register a document handler, all
       * document events reported by the CSS parser will be silently
       * ignored (this is the default behaviour implemented by
       * HandlerBase).</p>
       *
       * <p>Applications may register a new or different handler in the
       * middle of a parse, and the CSS parser must begin using the new
       * handler immediately.</p>
       *
       * @param handler The document handler.
       * @see DocumentHandler
       */
      public void setDocumentHandler(DocumentHandler handler) {
        parser.setDocumentHandler(handler);
      }
  
      public void setSelectorFactory(SelectorFactory selectorFactory) {
        parser.setSelectorFactory(selectorFactory);
      }
      public void setConditionFactory(ConditionFactory conditionFactory) {
        parser.setConditionFactory(conditionFactory);
      }
      
      /**
       * Allow an application to register an error event handler.
       *
       * <p>If the application does not register an error event handler,
       * all error events reported by the CSS parser will be silently
       * ignored, except for fatalError, which will throw a CSSException
       * (this is the default behaviour implemented by HandlerBase).</p>
       *
       * <p>Applications may register a new or different handler in the
       * middle of a parse, and the CSS parser must begin using the new
       * handler immediately.</p>
       *
       * @param handler The error handler.
       * @see ErrorHandler
       * @see CSSException
       */
      public void setErrorHandler(ErrorHandler handler) {
        parser.setErrorHandler(handler);
      }
      
      /**
       * Parse a CSS document.
       *
       * <p>The application can use this method to instruct the CSS parser
       * to begin parsing an CSS document from any valid input
       * source (a character stream, a byte stream, or a URI).</p>
       *
       * <p>Applications may not invoke this method while a parse is in
       * progress (they should create a new Parser instead for each
       * additional CSS document).  Once a parse is complete, an
       * application may reuse the same Parser object, possibly with a
       * different input source.</p>
       *
       * @param source The input source for the top-level of the
       *        CSS document.
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       * @see InputSource
       * @see #parseStyleSheet(java.lang.String)
       * @see #setDocumentHandler
       * @see #setErrorHandler
       */
      public void parseStyleSheet(InputSource source) 
        throws CSSException, IOException {
        parser.parseStyleSheet(source);
      }
      
      /**
       * Parse a CSS document from a URI.
       *
       * <p>This method is a shortcut for the common case of reading a document
       * from a URI.  It is the exact equivalent of the following:</p>
       *
       * <pre>
       * parse(new InputSource(uri));
       * </pre>
       *
       * <p>The URI must be fully resolved by the application before it is passed
       * to the parser.</p>
       *
       * @param uri The URI.
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       * @see #parseStyleSheet(InputSource) 
       */
      public void parseStyleSheet(String uri) throws CSSException, IOException {
        parser.parseStyleSheet(uri);
      }
  
      /**
       * Parse a CSS style declaration (without '{' and '}').
       *
       * @param styleValue The declaration.
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */
      public void parseStyleDeclaration(InputSource source) 
        throws CSSException, IOException {
        parser.parseStyleDeclaration(source);
      }
  
      /**
       * Parse a CSS style declaration (without '{' and '}').
       *
       * @param styleValue The declaration.
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */
      public void parseStyleDeclaration(String source) 
        throws CSSException, IOException {
        parser.parseStyleDeclaration
            (new InputSource(new StringReader(source)));
      }
  
  
      /**
       * Parse a CSS rule.
       *
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */
      public void parseRule(InputSource source) 
        throws CSSException, IOException {
        parser.parseRule(source);
      }
  
      /**
       * Parse a CSS rule.
       *
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */
      public void parseRule(String source) throws CSSException, IOException {
        parser.parseRule(new InputSource(new StringReader(source)));
      }
      
      /**
       * Parse a comma separated list of selectors.
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public SelectorList parseSelectors(InputSource source)
          throws CSSException, IOException {
        return parser.parseSelectors(source);
      }
  
      /**
       * Parse a comma separated list of selectors.
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public SelectorList parseSelectors(String source)
          throws CSSException, IOException {
        return parser.parseSelectors
            (new InputSource(new StringReader(source)));
      }
  
  
      /**
       * Parse a CSS property value.
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public LexicalUnit parsePropertyValue(InputSource source)
          throws CSSException, IOException {
        return parser.parsePropertyValue(source);
      }
  
      /**
       * Parse a CSS property value.
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public LexicalUnit parsePropertyValue(String source)
          throws CSSException, IOException {
        return parser.parsePropertyValue
            (new InputSource(new StringReader(source)));
      }
  
      
      /**
       * Parse a CSS priority value (e.g. "!important").
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public boolean parsePriority(InputSource source)
          throws CSSException, IOException {
        return parser.parsePriority(source);
      }
  
      /**
       * Parse a CSS priority value (e.g. "!important").
       * 
       * 
       * @exception CSSException Any CSS exception, possibly
       *            wrapping another exception.
       * @exception java.io.IOException An IO exception from the parser,
       *            possibly from a byte stream or character stream
       *            supplied by the application.
       */    
      public boolean parsePriority(String source)
          throws CSSException, IOException {
        return parser.parsePriority(new InputSource(new StringReader(source)));
      }
  }
  
  
  
  1.8       +6 -16     
xml-batik/sources/org/apache/batik/css/value/AbstractValueFactory.java
  
  Index: AbstractValueFactory.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/css/value/AbstractValueFactory.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractValueFactory.java 2001/11/09 17:12:02     1.7
  +++ AbstractValueFactory.java 2001/11/09 20:11:46     1.8
  @@ -16,6 +16,7 @@
   import org.apache.batik.css.CSSOMValue;
   
   import org.apache.batik.css.parser.ExtendedParser;
  +import org.apache.batik.css.parser.ExtendedParserWrapper;
   
   import org.w3c.css.sac.InputSource;
   import org.w3c.css.sac.LexicalUnit;
  @@ -27,7 +28,7 @@
    * This class provides a base implementation for every value factories.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: AbstractValueFactory.java,v 1.7 2001/11/09 17:12:02 tkormann Exp $
  + * @version $Id: AbstractValueFactory.java,v 1.8 2001/11/09 20:11:46 deweese Exp $
    */
   public abstract class AbstractValueFactory
       implements ValueFactory,
  @@ -36,20 +37,15 @@
       /**
        * The CSS parser.
        */
  -    protected Parser parser;
  +    protected ExtendedParser parser;
   
  -    /**
  -     * Indicates whether or not the CSS parser supports methods with String.
  -     */
  -    protected boolean isExtendedParser;
   
       /**
        * To creates a new ValueFactory object.
        * @param p The CSS parser used to parse the CSS texts.
        */
       protected AbstractValueFactory(Parser p) {
  -     parser = p;
  -     isExtendedParser = (parser instanceof ExtendedParser);
  +     parser = ExtendedParserWrapper.wrap(p);
       }
   
       /**
  @@ -63,8 +59,7 @@
        * Sets the parser used by this factory.
        */
       public void setParser(Parser p) {
  -        parser = p;
  -     isExtendedParser = (parser instanceof ExtendedParser);
  +     parser = ExtendedParserWrapper.wrap(p);
       }
   
       /**
  @@ -74,12 +69,7 @@
       public ImmutableValue createValue(String text) throws DOMException {
        try {
            LexicalUnit lu;
  -         if (isExtendedParser) {
  -             lu = ((ExtendedParser)parser).parsePropertyValue(text);
  -         } else {
  -             InputSource is = new InputSource(new StringReader(text));
  -             lu = parser.parsePropertyValue(is);
  -         }
  +         lu = parser.parsePropertyValue(text);
            return createValue(lu);
        } catch (IOException e) {
               // Should never happen
  
  
  

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

Reply via email to