001: /*
002:
003: ============================================================================
004: The Apache Software License, Version 1.1
005: ============================================================================
006:
007: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
008:
009: Redistribution and use in source and binary forms, with or without modifica-
010: tion, are permitted provided that the following conditions are met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright notice,
016: this list of conditions and the following disclaimer in the documentation
017: and/or other materials provided with the distribution.
018:
019: 3. The end-user documentation included with the redistribution, if any, must
020: include the following acknowledgment: "This product includes software
021: developed by the Apache Software Foundation (http://www.apache.org/)."
022: Alternately, this acknowledgment may appear in the software itself, if
023: and wherever such third-party acknowledgments normally appear.
024:
025: 4. The names "Batik" and "Apache Software Foundation" must not be
026: used to endorse or promote products derived from this software without
027: prior written permission. For written permission, please contact
028: apache@apache.org.
029:
030: 5. Products derived from this software may not be called "Apache", nor may
031: "Apache" appear in their name, without prior written permission of the
032: Apache Software Foundation.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
035: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
036: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
037: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
038: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
039: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
040: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
041: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: This software consists of voluntary contributions made by many individuals
046: on behalf of the Apache Software Foundation. For more information on the
047: Apache Software Foundation, please see <http://www.apache.org/>.
048:
049: */
050:
051: package org.apache.batik.css.parser;
052:
053: import java.io.IOException;
054: import java.io.StringReader;
055: import java.util.Locale;
056: import java.util.StringTokenizer;
057:
058: import org.w3c.css.sac.CSSException;
059: import org.w3c.css.sac.ConditionFactory;
060: import org.w3c.css.sac.DocumentHandler;
061: import org.w3c.css.sac.ErrorHandler;
062: import org.w3c.css.sac.InputSource;
063: import org.w3c.css.sac.LexicalUnit;
064: import org.w3c.css.sac.Parser;
065: import org.w3c.css.sac.SACMediaList;
066: import org.w3c.css.sac.SelectorFactory;
067: import org.w3c.css.sac.SelectorList;
068:
069: /**
070: * This class implements the {@link org.apache.batik.css.parser.ExtendedParser}
071: * interface by wrapping a standard {@link org.w3c.css.sac.Parser}.
072: *
073: * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
074: * @version $Id$
075: */
076: public class ExtendedParserWrapper implements ExtendedParser {
077:
078: /**
079: * This converts a standard @link org.w3c.css.sac.Parser into
080: * an Extended Parser. If it is already an ExtendedParser
081: * it will simply cast it and return, otherwise it will wrap it
082: * and return the result.
083: * @param p Parser to wrap.
084: * @return p as an ExtendedParser.
085: */
086: public static ExtendedParser wrap(Parser p) {
087: if (p instanceof ExtendedParser)
088: return (ExtendedParser) p;
089:
090: return new ExtendedParserWrapper(p);
091: }
092:
093: public Parser parser;
094:
095: public ExtendedParserWrapper(Parser parser) {
096: this .parser = parser;
097: }
098:
099: /**
100: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#getParserVersion()}.
101: */
102: public String getParserVersion() {
103: return parser.getParserVersion();
104: }
105:
106: /**
107: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#setLocale(Locale)}.
108: */
109: public void setLocale(Locale locale) throws CSSException {
110: parser.setLocale(locale);
111: }
112:
113: /**
114: * <b>SAC</b>: Implements {@link
115: * org.w3c.css.sac.Parser#setDocumentHandler(DocumentHandler)}.
116: */
117: public void setDocumentHandler(DocumentHandler handler) {
118: parser.setDocumentHandler(handler);
119: }
120:
121: /**
122: * <b>SAC</b>: Implements {@link
123: * org.w3c.css.sac.Parser#setSelectorFactory(SelectorFactory)}.
124: */
125: public void setSelectorFactory(SelectorFactory selectorFactory) {
126: parser.setSelectorFactory(selectorFactory);
127: }
128:
129: /**
130: * <b>SAC</b>: Implements {@link
131: * org.w3c.css.sac.Parser#setConditionFactory(ConditionFactory)}.
132: */
133: public void setConditionFactory(ConditionFactory conditionFactory) {
134: parser.setConditionFactory(conditionFactory);
135: }
136:
137: /**
138: * <b>SAC</b>: Implements {@link
139: * org.w3c.css.sac.Parser#setErrorHandler(ErrorHandler)}.
140: */
141: public void setErrorHandler(ErrorHandler handler) {
142: parser.setErrorHandler(handler);
143: }
144:
145: /**
146: * <b>SAC</b>: Implements {@link
147: * org.w3c.css.sac.Parser#parseStyleSheet(InputSource)}.
148: */
149: public void parseStyleSheet(InputSource source)
150: throws CSSException, IOException {
151: parser.parseStyleSheet(source);
152: }
153:
154: /**
155: * Parse a CSS document from a URI.
156: *
157: * <p>This method is a shortcut for the common case of reading a document
158: * from a URI. It is the exact equivalent of the following:</p>
159: *
160: * <pre>
161: * parse(new InputSource(uri));
162: * </pre>
163: *
164: * <p>The URI must be fully resolved by the application before it is passed
165: * to the parser.</p>
166: *
167: * @param uri The URI.
168: * @exception CSSException Any CSS exception, possibly
169: * wrapping another exception.
170: * @exception java.io.IOException An IO exception from the parser,
171: * possibly from a byte stream or character stream
172: * supplied by the application.
173: * @see #parseStyleSheet(InputSource)
174: */
175: public void parseStyleSheet(String uri) throws CSSException,
176: IOException {
177: parser.parseStyleSheet(uri);
178: }
179:
180: /**
181: * <b>SAC</b>: Implements {@link
182: * org.w3c.css.sac.Parser#parseStyleDeclaration(InputSource)}.
183: */
184: public void parseStyleDeclaration(InputSource source)
185: throws CSSException, IOException {
186: parser.parseStyleDeclaration(source);
187: }
188:
189: /**
190: * Parse a CSS style declaration (without '{' and '}').
191: *
192: * @param styleValue The declaration.
193: * @exception CSSException Any CSS exception, possibly
194: * wrapping another exception.
195: * @exception IOException An IO exception from the parser,
196: * possibly from a byte stream or character stream
197: * supplied by the application.
198: */
199: public void parseStyleDeclaration(String source)
200: throws CSSException, IOException {
201: parser.parseStyleDeclaration(new InputSource(new StringReader(
202: source)));
203: }
204:
205: /**
206: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseRule(InputSource)}.
207: */
208: public void parseRule(InputSource source) throws CSSException,
209: IOException {
210: parser.parseRule(source);
211: }
212:
213: /**
214: * Parse a CSS rule.
215: *
216: * @exception CSSException Any CSS exception, possibly
217: * wrapping another exception.
218: * @exception java.io.IOException An IO exception from the parser,
219: * possibly from a byte stream or character stream
220: * supplied by the application.
221: */
222: public void parseRule(String source) throws CSSException,
223: IOException {
224: parser.parseRule(new InputSource(new StringReader(source)));
225: }
226:
227: /**
228: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseSelectors(InputSource)}.
229: */
230: public SelectorList parseSelectors(InputSource source)
231: throws CSSException, IOException {
232: return parser.parseSelectors(source);
233: }
234:
235: /**
236: * Parse a comma separated list of selectors.
237: *
238: *
239: * @exception CSSException Any CSS exception, possibly
240: * wrapping another exception.
241: * @exception java.io.IOException An IO exception from the parser,
242: * possibly from a byte stream or character stream
243: * supplied by the application.
244: */
245: public SelectorList parseSelectors(String source)
246: throws CSSException, IOException {
247: return parser.parseSelectors(new InputSource(new StringReader(
248: source)));
249: }
250:
251: /**
252: * <b>SAC</b>: Implements
253: * {@link org.w3c.css.sac.Parser#parsePropertyValue(InputSource)}.
254: */
255: public LexicalUnit parsePropertyValue(InputSource source)
256: throws CSSException, IOException {
257: return parser.parsePropertyValue(source);
258: }
259:
260: /**
261: * Parse a CSS property value.
262: *
263: *
264: * @exception CSSException Any CSS exception, possibly
265: * wrapping another exception.
266: * @exception java.io.IOException An IO exception from the parser,
267: * possibly from a byte stream or character stream
268: * supplied by the application.
269: */
270: public LexicalUnit parsePropertyValue(String source)
271: throws CSSException, IOException {
272: return parser.parsePropertyValue(new InputSource(
273: new StringReader(source)));
274: }
275:
276: /**
277: * <b>SAC</b>: Implements
278: * {@link org.w3c.css.sac.Parser#parsePriority(InputSource)}.
279: */
280: public boolean parsePriority(InputSource source)
281: throws CSSException, IOException {
282: return parser.parsePriority(source);
283: }
284:
285: /**
286: * Implements {@link ExtendedParser#parseMedia(String)}.
287: */
288: public SACMediaList parseMedia(String mediaText)
289: throws CSSException, IOException {
290: CSSSACMediaList result = new CSSSACMediaList();
291: if (!"all".equalsIgnoreCase(mediaText)) {
292: StringTokenizer st = new StringTokenizer(mediaText, " ,");
293: while (st.hasMoreTokens()) {
294: result.append(st.nextToken());
295: }
296: }
297: return result;
298: }
299:
300: /**
301: * Parse a CSS priority value (e.g. "!important").
302: *
303: *
304: * @exception CSSException Any CSS exception, possibly
305: * wrapping another exception.
306: * @exception java.io.IOException An IO exception from the parser,
307: * possibly from a byte stream or character stream
308: * supplied by the application.
309: */
310: public boolean parsePriority(String source) throws CSSException,
311: IOException {
312: return parser.parsePriority(new InputSource(new StringReader(
313: source)));
314: }
315:
316: // BEGIN RAVE MODIFICATIONS
317: public int getLine() {
318: return -1;
319: }
320:
321: public int getColumn() {
322: return 0;
323: }
324: // END RAVE MODIFICATIONS
325: }
|