001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.css.parser;
020:
021: import java.io.IOException;
022: import java.io.StringReader;
023: import java.util.Locale;
024: import java.util.StringTokenizer;
025:
026: import org.w3c.css.sac.CSSException;
027: import org.w3c.css.sac.ConditionFactory;
028: import org.w3c.css.sac.DocumentHandler;
029: import org.w3c.css.sac.ErrorHandler;
030: import org.w3c.css.sac.InputSource;
031: import org.w3c.css.sac.LexicalUnit;
032: import org.w3c.css.sac.Parser;
033: import org.w3c.css.sac.SACMediaList;
034: import org.w3c.css.sac.SelectorFactory;
035: import org.w3c.css.sac.SelectorList;
036:
037: /**
038: * This class implements the {@link org.apache.batik.css.parser.ExtendedParser}
039: * interface by wrapping a standard {@link org.w3c.css.sac.Parser}.
040: *
041: * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
042: * @version $Id: ExtendedParserWrapper.java 475685 2006-11-16 11:16:05Z cam $
043: */
044: public class ExtendedParserWrapper implements ExtendedParser {
045:
046: /**
047: * This converts a standard @link org.w3c.css.sac.Parser into
048: * an Extended Parser. If it is already an ExtendedParser
049: * it will simply cast it and return, otherwise it will wrap it
050: * and return the result.
051: * @param p Parser to wrap.
052: * @return p as an ExtendedParser.
053: */
054: public static ExtendedParser wrap(Parser p) {
055: if (p instanceof ExtendedParser)
056: return (ExtendedParser) p;
057:
058: return new ExtendedParserWrapper(p);
059: }
060:
061: public Parser parser;
062:
063: public ExtendedParserWrapper(Parser parser) {
064: this .parser = parser;
065: }
066:
067: /**
068: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#getParserVersion()}.
069: */
070: public String getParserVersion() {
071: return parser.getParserVersion();
072: }
073:
074: /**
075: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#setLocale(Locale)}.
076: */
077: public void setLocale(Locale locale) throws CSSException {
078: parser.setLocale(locale);
079: }
080:
081: /**
082: * <b>SAC</b>: Implements {@link
083: * org.w3c.css.sac.Parser#setDocumentHandler(DocumentHandler)}.
084: */
085: public void setDocumentHandler(DocumentHandler handler) {
086: parser.setDocumentHandler(handler);
087: }
088:
089: /**
090: * <b>SAC</b>: Implements {@link
091: * org.w3c.css.sac.Parser#setSelectorFactory(SelectorFactory)}.
092: */
093: public void setSelectorFactory(SelectorFactory selectorFactory) {
094: parser.setSelectorFactory(selectorFactory);
095: }
096:
097: /**
098: * <b>SAC</b>: Implements {@link
099: * org.w3c.css.sac.Parser#setConditionFactory(ConditionFactory)}.
100: */
101: public void setConditionFactory(ConditionFactory conditionFactory) {
102: parser.setConditionFactory(conditionFactory);
103: }
104:
105: /**
106: * <b>SAC</b>: Implements {@link
107: * org.w3c.css.sac.Parser#setErrorHandler(ErrorHandler)}.
108: */
109: public void setErrorHandler(ErrorHandler handler) {
110: parser.setErrorHandler(handler);
111: }
112:
113: /**
114: * <b>SAC</b>: Implements {@link
115: * org.w3c.css.sac.Parser#parseStyleSheet(InputSource)}.
116: */
117: public void parseStyleSheet(InputSource source)
118: throws CSSException, IOException {
119: parser.parseStyleSheet(source);
120: }
121:
122: /**
123: * Parse a CSS document from a URI.
124: *
125: * <p>This method is a shortcut for the common case of reading a document
126: * from a URI. It is the exact equivalent of the following:</p>
127: *
128: * <pre>
129: * parse(new InputSource(uri));
130: * </pre>
131: *
132: * <p>The URI must be fully resolved by the application before it is passed
133: * to the parser.</p>
134: *
135: * @param uri The URI.
136: * @exception CSSException Any CSS exception, possibly
137: * wrapping another exception.
138: * @exception java.io.IOException An IO exception from the parser,
139: * possibly from a byte stream or character stream
140: * supplied by the application.
141: * @see #parseStyleSheet(InputSource)
142: */
143: public void parseStyleSheet(String uri) throws CSSException,
144: IOException {
145: parser.parseStyleSheet(uri);
146: }
147:
148: /**
149: * <b>SAC</b>: Implements {@link
150: * org.w3c.css.sac.Parser#parseStyleDeclaration(InputSource)}.
151: */
152: public void parseStyleDeclaration(InputSource source)
153: throws CSSException, IOException {
154: parser.parseStyleDeclaration(source);
155: }
156:
157: /**
158: * Parse a CSS style declaration (without '{' and '}').
159: *
160: * @param source The declaration.
161: * @exception CSSException Any CSS exception, possibly
162: * wrapping another exception.
163: * @exception IOException An IO exception from the parser,
164: * possibly from a byte stream or character stream
165: * supplied by the application.
166: */
167: public void parseStyleDeclaration(String source)
168: throws CSSException, IOException {
169: parser.parseStyleDeclaration(new InputSource(new StringReader(
170: source)));
171: }
172:
173: /**
174: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseRule(InputSource)}.
175: */
176: public void parseRule(InputSource source) throws CSSException,
177: IOException {
178: parser.parseRule(source);
179: }
180:
181: /**
182: * Parse a CSS rule.
183: *
184: * @exception CSSException Any CSS exception, possibly
185: * wrapping another exception.
186: * @exception java.io.IOException An IO exception from the parser,
187: * possibly from a byte stream or character stream
188: * supplied by the application.
189: */
190: public void parseRule(String source) throws CSSException,
191: IOException {
192: parser.parseRule(new InputSource(new StringReader(source)));
193: }
194:
195: /**
196: * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseSelectors(InputSource)}.
197: */
198: public SelectorList parseSelectors(InputSource source)
199: throws CSSException, IOException {
200: return parser.parseSelectors(source);
201: }
202:
203: /**
204: * Parse a comma separated list of selectors.
205: *
206: *
207: * @exception CSSException Any CSS exception, possibly
208: * wrapping another exception.
209: * @exception java.io.IOException An IO exception from the parser,
210: * possibly from a byte stream or character stream
211: * supplied by the application.
212: */
213: public SelectorList parseSelectors(String source)
214: throws CSSException, IOException {
215: return parser.parseSelectors(new InputSource(new StringReader(
216: source)));
217: }
218:
219: /**
220: * <b>SAC</b>: Implements
221: * {@link org.w3c.css.sac.Parser#parsePropertyValue(InputSource)}.
222: */
223: public LexicalUnit parsePropertyValue(InputSource source)
224: throws CSSException, IOException {
225: return parser.parsePropertyValue(source);
226: }
227:
228: /**
229: * Parse a CSS property value.
230: *
231: *
232: * @exception CSSException Any CSS exception, possibly
233: * wrapping another exception.
234: * @exception java.io.IOException An IO exception from the parser,
235: * possibly from a byte stream or character stream
236: * supplied by the application.
237: */
238: public LexicalUnit parsePropertyValue(String source)
239: throws CSSException, IOException {
240: return parser.parsePropertyValue(new InputSource(
241: new StringReader(source)));
242: }
243:
244: /**
245: * <b>SAC</b>: Implements
246: * {@link org.w3c.css.sac.Parser#parsePriority(InputSource)}.
247: */
248: public boolean parsePriority(InputSource source)
249: throws CSSException, IOException {
250: return parser.parsePriority(source);
251: }
252:
253: /**
254: * Implements {@link ExtendedParser#parseMedia(String)}.
255: */
256: public SACMediaList parseMedia(String mediaText)
257: throws CSSException, IOException {
258: CSSSACMediaList result = new CSSSACMediaList();
259: if (!"all".equalsIgnoreCase(mediaText)) {
260: StringTokenizer st = new StringTokenizer(mediaText, " ,");
261: while (st.hasMoreTokens()) {
262: result.append(st.nextToken());
263: }
264: }
265: return result;
266: }
267:
268: /**
269: * Parse a CSS priority value (e.g. "!important").
270: *
271: *
272: * @exception CSSException Any CSS exception, possibly
273: * wrapping another exception.
274: * @exception java.io.IOException An IO exception from the parser,
275: * possibly from a byte stream or character stream
276: * supplied by the application.
277: */
278: public boolean parsePriority(String source) throws CSSException,
279: IOException {
280: return parser.parsePriority(new InputSource(new StringReader(
281: source)));
282: }
283: }
|