Author: matzew
Date: Fri Jun 9 16:01:34 2006
New Revision: 413186
URL: http://svn.apache.org/viewvc?rev=413186&view=rev
Log:
applied patch for ADFFACES-19 and ADFFACES-20. Thanks to Jeanne Waldman (jeanne
DOT waldman AT oracle DOT com)
Modified:
incubator/adffaces/trunk/adf-faces/adf-faces-impl/src/main/java/org/apache/myfaces/adfinternal/skin/SkinCSSParser.java
Modified:
incubator/adffaces/trunk/adf-faces/adf-faces-impl/src/main/java/org/apache/myfaces/adfinternal/skin/SkinCSSParser.java
URL:
http://svn.apache.org/viewvc/incubator/adffaces/trunk/adf-faces/adf-faces-impl/src/main/java/org/apache/myfaces/adfinternal/skin/SkinCSSParser.java?rev=413186&r1=413185&r2=413186&view=diff
==============================================================================
---
incubator/adffaces/trunk/adf-faces/adf-faces-impl/src/main/java/org/apache/myfaces/adfinternal/skin/SkinCSSParser.java
(original)
+++
incubator/adffaces/trunk/adf-faces/adf-faces-impl/src/main/java/org/apache/myfaces/adfinternal/skin/SkinCSSParser.java
Fri Jun 9 16:01:34 2006
@@ -13,380 +13,399 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.myfaces.adfinternal.skin;
-
-import java.io.IOException;
-import java.io.Reader;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.myfaces.adf.logging.ADFLogger;
-
-
-/**
- * This parses a skin css file into namespace map and
- * selector/properties.
- */
-public class SkinCSSParser
-{
-
-
- public SkinCSSParser()
- {
- }
-
-
- public void parseCSSDocument(
- Reader in,
- SkinCSSDocumentHandler documentHandler)
- {
-
- try
- {
- _documentHandler = documentHandler;
- _scanner = new CSSScanner(in);
- _documentHandler.startDocument();
- List selectorList = null;
-
- // start scanning the document
- // return comments /* xxx */
- // return @rules, which end with ';'
- // return selectors, which end with a {
- // return properties, which end with a }
- _currentType = _nextIgnoreSpaces();
-
- while (_currentType != CSSLexicalUnits.EOF)
- {
- if (_currentType == CSSLexicalUnits.COMMENT)
- _documentHandler.comment(_scanner.getStringValue());
- else if (_currentType == CSSLexicalUnits.AT_KEYWORD)
- _documentHandler.atRule(_scanner.getStringValue());
- else if (_currentType == CSSLexicalUnits.LEFT_CURLY_BRACE)
- {
- _documentHandler.startSelector();
- selectorList = _parseSelectorString(_scanner.getStringValue());
- }
- else if (_currentType == CSSLexicalUnits.RIGHT_CURLY_BRACE)
- {
- String properties = _scanner.getStringValue();
- _handlePropertiesString(properties);
- if (selectorList == null)
- {
- if (_LOG.isWarning())
- {
- _LOG.warning("Ignoring properties " + properties +
- " because there is no corresponding selector.");
- }
- }
- _documentHandler.endSelector(selectorList);
- }
- _nextIgnoreSpaces();
- }
- }
- finally
- {
- _documentHandler.endDocument();
- }
- }
-
- /**
- * given a string that denotes the selectors in a css file, parse this
- * further into a list of selectors.
- * (the selectors are deliminated by commas)
- */
- private List _parseSelectorString(
- String selectors)
- {
- // give a list of selectors, deliminated by commas, parse into a List.
- // loop thru each character until I get to a left bracket.
- if (selectors == null)
- return null;
-
- List selectorList = new ArrayList();
-
- // pull apart by the regexp that means
- // (zero or more whitespace) then (comma) then (zero or more whitespace)
- String[] selector = selectors.split("\\s*,\\s*");
- String trimmedSelector;
- for (int i=0; i < selector.length; i++)
- {
- // the first selector might have extra }
- // this is a common typo, to have extra }s.
- if (i == 0)
- trimmedSelector = _trimChar(selector[i].trim(), '}');
- else
- trimmedSelector = selector[i].trim();
-
- selectorList.add(trimmedSelector);
- }
- return selectorList;
- }
-
- /**
- * given a string that denotes the properties of one or more selectors,
- * parse further into name/value pairs and call documentHandler's property
- * callback.
- */
- private void _handlePropertiesString(
- String properties)
- {
- if (properties == null)
- return;
-
- String[] property = properties.split("\\s*;\\s*");
-
- for (int i=0; i < property.length; i++)
- {
- int indexOfColon = property[i].indexOf(':');
- if ((indexOfColon > -1) && (indexOfColon < property[i].length()))
- {
- String name = property[i].substring(0, indexOfColon);
- String value = property[i].substring(indexOfColon+1);
- _documentHandler.property((name.trim()), value.trim());
-
- }
- }
-
-
- }
-
- private static String _trimChar(
- String in,
- char c)
- {
- int len = in.length();
- char currentChar = in.charAt(0);
- if (currentChar != c)
- return in;
-
- for (int i=1; i < len; i++)
- {
- currentChar = in.charAt(i);
- if (currentChar != c)
- {
-
- return in.substring(i);
- }
- }
- return in;
-
- }
-
- // ignores spaces.
- private int _nextIgnoreSpaces()
- {
- _currentType = _scanner.getNextToken();
- while (_currentType == CSSLexicalUnits.SPACE)
- {
- _currentType = _scanner.getNextToken();
- }
- return _currentType;
- }
-
-
-
- // This class builds up tokens for SPACE, COMMENT, AT_RULE,
- // LEFT_CURLY_BRACE (selectorList), and RIGHT_CURLY_BRACE (properties)
- // A token is stored in the _buffer object.
- private static class CSSScanner
- {
- public CSSScanner(Reader reader)
- {
- _reader = reader;
- }
-
- public String getStringValue()
- {
- if (_end <= 0)
- return null;
- else
- return new String(_buffer, 0, _end);
- }
-
-
- // get the next token in the buffer and return the type
- public int getNextToken()
- {
- _position = 0;
- _fillToken();
-
- _end = _position;
-
- // strip off the final brace if needed
- if (_type == CSSLexicalUnits.RIGHT_CURLY_BRACE ||
- _type == CSSLexicalUnits.LEFT_CURLY_BRACE)
- _end--;
-
- if (_currentChar == -1)
- return CSSLexicalUnits.EOF;
- return _type;
- }
-
-
-
- private void _fillToken()
- {
- while (true)
- {
- _nextChar();
- switch (_currentChar)
- {
- case -1:
- _type = CSSLexicalUnits.EOF;
- break;
-
-
- case ' ':
- case '\t':
- case '\n':
- case '\f':
- case '\r':
- if (_type != CSSLexicalUnits.LEFT_CURLY_BRACE)
- {
- _type = CSSLexicalUnits.SPACE;
- return;
- }
- // fall through to LEFT_CURLY_BRACE
-
-
- case '/':
- // check for comment. If it is a comment, set the type and return
- // if it isn't a comment, keep looping to get more characters.
- _nextChar();
- if (_currentChar == '*')
- {
- // WE ARE IN A COMMENT
- // loop and get characters into buffer until we get '*/'
-
- _nextChar();
- int prevChar;
- while (_currentChar != -1)
- {
-
- prevChar = _currentChar;
- _nextChar();
- if ((prevChar == '*') && (_currentChar == '/'))
- break;
- }
-
- _type = CSSLexicalUnits.COMMENT;
- return;
-
- }
- // wasn't a comment, so keep going on, filling the buffer with
- // each _nextChar call.
- break;
-
-
-
- case '@':
- // found @. keep getting characters until we get a ; or end of
file.
- _nextChar();
- while ((_currentChar != -1) && (_currentChar != ';'))
- {
- _nextChar();
- }
-
- _type = CSSLexicalUnits.AT_KEYWORD;
- return;
-
- default:
- if (_type == CSSLexicalUnits.LEFT_CURLY_BRACE)
- {
- // these are the properties,
- // keep going until we have all the properties
- while ((_currentChar != -1) && (_currentChar != '}'))
- {
- _nextChar();
- }
- _type = CSSLexicalUnits.RIGHT_CURLY_BRACE;
- }
- else
- {
- while ((_currentChar != -1) && (_currentChar != '{'))
- {
- _nextChar();
- }
- _type = CSSLexicalUnits.LEFT_CURLY_BRACE;
- }
- return;
-
- } // end switch
-
-
-
- if (_currentChar == -1)
- {
- _type = CSSLexicalUnits.EOF;
- return;
- }
- }
-
- }
-
-
- // fill buffer with one more character
- private void _nextChar()
- {
- try
- {
- _currentChar = _reader.read();
- }
- catch (IOException e)
- {
- if (_LOG.isSevere())
- {
- _LOG.severe("Error reading from the skin css file", e);
- }
- _currentChar = -1;
- return;
- }
- // need to make sure buffer doesn't go over its size
- if (_buffer.length <= _position)
- {
- // increase buffer size by 50%
- char[] tmp = new char[_buffer.length + (_buffer.length/2)];
- // copy over buffer to new buffer
- for (int i=0; i < _buffer.length; i++)
- tmp[i] = _buffer[i];
- // pt _buffer to bigger buffer.
- _buffer = tmp;
-
- }
-
- _buffer[_position++] = (char)_currentChar;
-
- }
-
- private Reader _reader;
- // buffer parameters
- private char[] _buffer = new char[1024];
- private int _position;
- private int _end;
- // type of token (it will be a CSSLexicalUnits constant)
- private int _type;
- // current character. -1 means EOF
- private int _currentChar;
- }
-
- /**
- * constants that we use to keep track of what type of token of the css file
- * we have parsed.
- */
- private static class CSSLexicalUnits
- {
- public static final int EOF = 0;
- public static final int LEFT_CURLY_BRACE = 1;
- public static final int RIGHT_CURLY_BRACE = 2;
- public static final int SPACE = 3;
- public static final int COMMENT = 4;
- public static final int AT_KEYWORD = 5;
- }
-
- private SkinCSSDocumentHandler _documentHandler;
- private CSSScanner _scanner;
- private int _currentType;
- private static final ADFLogger _LOG =
ADFLogger.createADFLogger(SkinCSSParser.class);
-
-
+package org.apache.myfaces.adfinternal.skin;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.myfaces.adf.logging.ADFLogger;
+
+
+/**
+ * This parses a skin css file into namespace map and
+ * selector/properties.
+ */
+public class SkinCSSParser
+{
+
+
+ public SkinCSSParser()
+ {
+ }
+
+
+ public void parseCSSDocument(
+ Reader in,
+ SkinCSSDocumentHandler documentHandler)
+ {
+
+ try
+ {
+ _documentHandler = documentHandler;
+ _scanner = new CSSScanner(in);
+ _documentHandler.startDocument();
+ List selectorList = null;
+
+ // start scanning the document
+ // return comments /* xxx */
+ // return @rules, which end with ';'
+ // return selectors, which end with a {
+ // return properties, which end with a }
+ _currentType = _nextIgnoreSpaces();
+
+ while (_currentType != CSSLexicalUnits.EOF)
+ {
+ if (_currentType == CSSLexicalUnits.COMMENT)
+ _documentHandler.comment(_scanner.getStringValue());
+ else if (_currentType == CSSLexicalUnits.AT_KEYWORD)
+ _documentHandler.atRule(_scanner.getStringValue());
+ else if (_currentType == CSSLexicalUnits.LEFT_CURLY_BRACE)
+ {
+ _documentHandler.startSelector();
+ selectorList = _parseSelectorString(_scanner.getStringValue());
+ }
+ else if (_currentType == CSSLexicalUnits.RIGHT_CURLY_BRACE)
+ {
+ String properties = _scanner.getStringValue();
+ _handlePropertiesString(properties);
+ if (selectorList == null)
+ {
+ if (_LOG.isWarning())
+ {
+ _LOG.warning("Ignoring properties " + properties +
+ " because there is no corresponding selector.");
+ }
+ }
+ _documentHandler.endSelector(selectorList);
+ }
+ _nextIgnoreSpaces();
+ }
+ }
+ finally
+ {
+ _documentHandler.endDocument();
+ }
+ }
+
+ /**
+ * given a string that denotes the selectors in a css file, parse this
+ * further into a list of selectors.
+ * (the selectors are deliminated by commas)
+ */
+ private List _parseSelectorString(
+ String selectors)
+ {
+ // give a list of selectors, deliminated by commas, parse into a List.
+ // loop thru each character until I get to a left bracket.
+ if (selectors == null)
+ return null;
+
+ List selectorList = new ArrayList();
+
+ // pull apart by the regexp that means
+ // (zero or more whitespace) then (comma) then (zero or more whitespace)
+ String[] selector = selectors.split("\\s*,\\s*");
+ String trimmedSelector;
+ for (int i=0; i < selector.length; i++)
+ {
+ // the first selector might have extra }
+ // this is a common typo, to have extra }s.
+ if (i == 0)
+ trimmedSelector = _trimChar(selector[i].trim(), '}');
+ else
+ trimmedSelector = selector[i].trim();
+
+ selectorList.add(trimmedSelector);
+ }
+ return selectorList;
+ }
+
+ /**
+ * given a string that denotes the properties of one or more selectors,
+ * parse further into name/value pairs and call documentHandler's property
+ * callback.
+ */
+ private void _handlePropertiesString(
+ String properties)
+ {
+ if (properties == null)
+ return;
+
+ // first, parse out any comments
+ Matcher matcher = _COMMENT_PATTERN.matcher(properties);
+ properties = matcher.replaceAll("");
+
+ String[] property = properties.split("\\s*;\\s*");
+
+ for (int i=0; i < property.length; i++)
+ {
+ int indexOfColon = property[i].indexOf(':');
+ if ((indexOfColon > -1) && (indexOfColon < property[i].length()))
+ {
+ String name = property[i].substring(0, indexOfColon);
+ String value = property[i].substring(indexOfColon+1);
+ _documentHandler.property((name.trim()), value.trim());
+
+ }
+ }
+
+
+ }
+
+ private static String _trimChar(
+ String in,
+ char c)
+ {
+ int len = in.length();
+ char currentChar = in.charAt(0);
+ if (currentChar != c)
+ return in;
+
+ for (int i=1; i < len; i++)
+ {
+ currentChar = in.charAt(i);
+ if (currentChar != c)
+ {
+
+ return in.substring(i);
+ }
+ }
+ return in;
+
+ }
+
+ // ignores spaces.
+ private int _nextIgnoreSpaces()
+ {
+ _currentType = _scanner.getNextToken();
+ while (_currentType == CSSLexicalUnits.SPACE)
+ {
+ _currentType = _scanner.getNextToken();
+ }
+ return _currentType;
+ }
+
+
+
+ // This class builds up tokens for SPACE, COMMENT, AT_RULE,
+ // LEFT_CURLY_BRACE (selectorList), and RIGHT_CURLY_BRACE (properties)
+ // A token is stored in the _buffer object.
+ private static class CSSScanner
+ {
+ public CSSScanner(Reader reader)
+ {
+ _reader = reader;
+ }
+
+ public String getStringValue()
+ {
+ if (_end <= 0)
+ return null;
+ else
+ return new String(_buffer, 0, _end);
+ }
+
+
+ // get the next token in the buffer and return the type
+ public int getNextToken()
+ {
+ _position = 0;
+ _fillToken();
+
+ _end = _position;
+
+ // strip off the final brace if needed
+ if (_type == CSSLexicalUnits.RIGHT_CURLY_BRACE ||
+ _type == CSSLexicalUnits.LEFT_CURLY_BRACE)
+ _end--;
+
+ if (_currentChar == -1)
+ return CSSLexicalUnits.EOF;
+ return _type;
+ }
+
+
+
+ private void _fillToken()
+ {
+ while (true)
+ {
+ _nextChar();
+ switch (_currentChar)
+ {
+ case -1:
+ _type = CSSLexicalUnits.EOF;
+ break;
+
+
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\f':
+ case '\r':
+ if (_type != CSSLexicalUnits.LEFT_CURLY_BRACE)
+ {
+ _type = CSSLexicalUnits.SPACE;
+ return;
+ }
+ // fall through to LEFT_CURLY_BRACE
+
+
+ case '/':
+ if (_type != CSSLexicalUnits.LEFT_CURLY_BRACE)
+ {
+ // check for comment. If it is a comment, set the type and return
+ // if it isn't a comment, keep looping to get more characters.
+ _nextChar();
+ if (_currentChar == '*')
+ {
+ // WE ARE IN A COMMENT
+ // loop and get characters into buffer until we get '*/'
+
+ _nextChar();
+ int prevChar;
+ while (_currentChar != -1)
+ {
+
+ prevChar = _currentChar;
+ _nextChar();
+ if ((prevChar == '*') && (_currentChar == '/'))
+ break;
+ }
+
+ _type = CSSLexicalUnits.COMMENT;
+ return;
+
+ }
+ // wasn't a comment, so keep going on, filling the buffer with
+ // each _nextChar call.
+ break;
+ }
+
+
+
+ case '@':
+ if (_type != CSSLexicalUnits.LEFT_CURLY_BRACE)
+ {
+ // found @. keep getting characters until we get a ; or end of
file.
+ _nextChar();
+ while ((_currentChar != -1) && (_currentChar != ';'))
+ {
+ _nextChar();
+ }
+
+ _type = CSSLexicalUnits.AT_KEYWORD;
+ return;
+ }
+
+ default:
+ if (_type == CSSLexicalUnits.LEFT_CURLY_BRACE)
+ {
+ // these are the properties,
+ // keep going until we have all the properties
+ while ((_currentChar != -1) && (_currentChar != '}'))
+ {
+ _nextChar();
+ }
+ _type = CSSLexicalUnits.RIGHT_CURLY_BRACE;
+ }
+ else
+ {
+ while ((_currentChar != -1) && (_currentChar != '{'))
+ {
+ _nextChar();
+ }
+ _type = CSSLexicalUnits.LEFT_CURLY_BRACE;
+ }
+ return;
+
+ } // end switch
+
+
+
+ if (_currentChar == -1)
+ {
+ _type = CSSLexicalUnits.EOF;
+ return;
+ }
+ }
+
+ }
+
+
+ // fill buffer with one more character
+ private void _nextChar()
+ {
+ try
+ {
+ _currentChar = _reader.read();
+ }
+ catch (IOException e)
+ {
+ if (_LOG.isSevere())
+ {
+ _LOG.severe("Error reading from the skin css file", e);
+ }
+ _currentChar = -1;
+ return;
+ }
+ // need to make sure buffer doesn't go over its size
+ if (_buffer.length <= _position)
+ {
+ // increase buffer size by 50%
+ char[] tmp = new char[_buffer.length + (_buffer.length/2)];
+ // copy over buffer to new buffer
+ for (int i=0; i < _buffer.length; i++)
+ tmp[i] = _buffer[i];
+ // pt _buffer to bigger buffer.
+ _buffer = tmp;
+
+ }
+
+ _buffer[_position++] = (char)_currentChar;
+
+ }
+
+ private Reader _reader;
+ // buffer parameters
+ private char[] _buffer = new char[1024];
+ private int _position;
+ private int _end;
+ // type of token (it will be a CSSLexicalUnits constant)
+ private int _type;
+ // current character. -1 means EOF
+ private int _currentChar;
+ }
+
+ /**
+ * constants that we use to keep track of what type of token of the css file
+ * we have parsed.
+ */
+ private static class CSSLexicalUnits
+ {
+ public static final int EOF = 0;
+ public static final int LEFT_CURLY_BRACE = 1;
+ public static final int RIGHT_CURLY_BRACE = 2;
+ public static final int SPACE = 3;
+ public static final int COMMENT = 4;
+ public static final int AT_KEYWORD = 5;
+ }
+
+ private SkinCSSDocumentHandler _documentHandler;
+ private CSSScanner _scanner;
+ private int _currentType;
+ // this is the pattern for finding comments. We want to strip out
+ // comments from the properties, and we use this pattern to do it.
+ private static final Pattern _COMMENT_PATTERN =
+ Pattern.compile("(?s)/\\*.*?\\*/");
+
+ private static final ADFLogger _LOG =
+ ADFLogger.createADFLogger(SkinCSSParser.class);
+
+
}