KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > parser > ExtendedParserWrapper


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.parser;
19
20 import java.io.IOException JavaDoc;
21 import java.io.StringReader JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.w3c.css.sac.CSSException;
26 import org.w3c.css.sac.ConditionFactory;
27 import org.w3c.css.sac.DocumentHandler;
28 import org.w3c.css.sac.ErrorHandler;
29 import org.w3c.css.sac.InputSource;
30 import org.w3c.css.sac.LexicalUnit;
31 import org.w3c.css.sac.Parser;
32 import org.w3c.css.sac.SACMediaList;
33 import org.w3c.css.sac.SelectorFactory;
34 import org.w3c.css.sac.SelectorList;
35
36 /**
37  * This class implements the {@link org.apache.batik.css.parser.ExtendedParser}
38  * interface by wrapping a standard {@link org.w3c.css.sac.Parser}.
39  *
40  * @author <a HREF="mailto:deweese@apache.org">Thomas DeWeese</a>
41  * @version $Id: ExtendedParserWrapper.java,v 1.7 2005/03/27 08:58:31 cam Exp $
42  */

43 public class ExtendedParserWrapper implements ExtendedParser {
44
45     /**
46      * This converts a standard @link org.w3c.css.sac.Parser into
47      * an Extended Parser. If it is already an ExtendedParser
48      * it will simply cast it and return, otherwise it will wrap it
49      * and return the result.
50      * @param p Parser to wrap.
51      * @return p as an ExtendedParser.
52      */

53     public static ExtendedParser wrap(Parser p) {
54     if (p instanceof ExtendedParser)
55         return (ExtendedParser)p;
56
57     return new ExtendedParserWrapper(p);
58     }
59
60
61     public Parser parser;
62
63     public ExtendedParserWrapper(Parser parser) {
64     this.parser = parser;
65     }
66     
67     /**
68      * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#getParserVersion()}.
69      */

70     public String JavaDoc getParserVersion() {
71     return parser.getParserVersion();
72     }
73     
74     /**
75      * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#setLocale(Locale)}.
76      */

77     public void setLocale(Locale JavaDoc locale) throws CSSException {
78     parser.setLocale(locale);
79     }
80
81     /**
82      * <b>SAC</b>: Implements {@link
83      * org.w3c.css.sac.Parser#setDocumentHandler(DocumentHandler)}.
84      */

85     public void setDocumentHandler(DocumentHandler handler) {
86     parser.setDocumentHandler(handler);
87     }
88
89     /**
90      * <b>SAC</b>: Implements {@link
91      * org.w3c.css.sac.Parser#setSelectorFactory(SelectorFactory)}.
92      */

93     public void setSelectorFactory(SelectorFactory selectorFactory) {
94     parser.setSelectorFactory(selectorFactory);
95     }
96
97     /**
98      * <b>SAC</b>: Implements {@link
99      * 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 JavaDoc {
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 JavaDoc uri) throws CSSException, IOException JavaDoc {
144     parser.parseStyleSheet(uri);
145     }
146
147     /**
148      * <b>SAC</b>: Implements {@link
149      * org.w3c.css.sac.Parser#parseStyleDeclaration(InputSource)}.
150      */

151     public void parseStyleDeclaration(InputSource source)
152     throws CSSException, IOException JavaDoc {
153     parser.parseStyleDeclaration(source);
154     }
155
156     /**
157      * Parse a CSS style declaration (without '{' and '}').
158      *
159      * @param source The declaration.
160      * @exception CSSException Any CSS exception, possibly
161      * wrapping another exception.
162      * @exception IOException An IO exception from the parser,
163      * possibly from a byte stream or character stream
164      * supplied by the application.
165      */

166     public void parseStyleDeclaration(String JavaDoc source)
167     throws CSSException, IOException JavaDoc {
168     parser.parseStyleDeclaration
169         (new InputSource(new StringReader JavaDoc(source)));
170     }
171
172
173     /**
174      * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseRule(InputSource)}.
175      */

176     public void parseRule(InputSource source)
177     throws CSSException, IOException JavaDoc {
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 JavaDoc source) throws CSSException, IOException JavaDoc {
191     parser.parseRule(new InputSource(new StringReader JavaDoc(source)));
192     }
193     
194     /**
195      * <b>SAC</b>: Implements {@link org.w3c.css.sac.Parser#parseSelectors(InputSource)}.
196      */

197     public SelectorList parseSelectors(InputSource source)
198         throws CSSException, IOException JavaDoc {
199     return parser.parseSelectors(source);
200     }
201
202     /**
203      * Parse a comma separated list of selectors.
204      *
205      *
206      * @exception CSSException Any CSS exception, possibly
207      * wrapping another exception.
208      * @exception java.io.IOException An IO exception from the parser,
209      * possibly from a byte stream or character stream
210      * supplied by the application.
211      */

212     public SelectorList parseSelectors(String JavaDoc source)
213         throws CSSException, IOException JavaDoc {
214     return parser.parseSelectors
215         (new InputSource(new StringReader JavaDoc(source)));
216     }
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 JavaDoc {
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 JavaDoc source)
239         throws CSSException, IOException JavaDoc {
240     return parser.parsePropertyValue
241         (new InputSource(new StringReader JavaDoc(source)));
242     }
243
244     
245     /**
246      * <b>SAC</b>: Implements
247      * {@link org.w3c.css.sac.Parser#parsePriority(InputSource)}.
248      */

249     public boolean parsePriority(InputSource source)
250         throws CSSException, IOException JavaDoc {
251     return parser.parsePriority(source);
252     }
253
254     /**
255      * Implements {@link ExtendedParser#parseMedia(String)}.
256      */

257     public SACMediaList parseMedia(String JavaDoc mediaText)
258         throws CSSException, IOException JavaDoc {
259         CSSSACMediaList result = new CSSSACMediaList();
260         if (!"all".equalsIgnoreCase(mediaText)) {
261             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mediaText, " ,");
262             while (st.hasMoreTokens()) {
263                 result.append(st.nextToken());
264             }
265         }
266         return result;
267     }
268
269     /**
270      * Parse a CSS priority value (e&#x2e;g&#x2e; "&#x21;important").
271      *
272      *
273      * @exception CSSException Any CSS exception, possibly
274      * wrapping another exception.
275      * @exception java.io.IOException An IO exception from the parser,
276      * possibly from a byte stream or character stream
277      * supplied by the application.
278      */

279     public boolean parsePriority(String JavaDoc source)
280         throws CSSException, IOException JavaDoc {
281     return parser.parsePriority(new InputSource(new StringReader JavaDoc(source)));
282     }
283 }
284
Popular Tags