2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text.html;
44 * Parses a CSS document. This works by way of a delegate that implements the
45 * CSSParserCallback interface. The delegate is notified of the following
47 * - Import statement: handleImport
48 * - Selectors handleSelector. This is invoked for each string. For example if
49 * the Reader contained p, bar , a {}, the delegate would be notified 4 times,
50 * for 'p,' 'bar' ',' and 'a'.
51 * - When a rule starts, startRule
52 * - Properties in the rule via the handleProperty. This
53 * is invoked one per property/value key, eg font size: foo;, would cause the
54 * delegate to be notified once with a value of 'font size'.
55 * - Values in the rule via the handleValue, this is notified for the total value.
56 * - When a rule ends, endRule
58 * @author Lillian Angel (langel@redhat.com)
64 * Receives all information about the CSS document structure while parsing it.
65 * The methods are invoked by parser.
67 static interface CSSParserCallback
70 * Handles the import statment in the document.
72 * @param imp - the import string
74 public abstract void handleImport(String imp);
77 * Called when the start of a rule is encountered.
79 public abstract void startRule();
82 * Called when the end of a rule is encountered.
84 public abstract void endRule();
87 * Handles the selector of a rule.
89 * @param selector - the selector in the rule
91 public abstract void handleSelector(String selector);
94 * Handles the properties in the document.
96 * @param property - the property in the document.
98 public abstract void handleProperty(String property);
101 * Handles the values in the document.
103 * @param value - the value to handle.
105 public abstract void handleValue(String value);
110 * The identifier of the rule.
112 private static final int IDENTIFIER = 1;
117 private static final int BRACKET_OPEN = 2;
122 private static final int BRACKET_CLOSE = 3;
127 private static final int BRACE_OPEN = 4;
132 private static final int BRACE_CLOSE = 5;
135 * The open parentheses.
137 private static final int PAREN_OPEN = 6;
140 * The close parentheses.
142 private static final int PAREN_CLOSE = 7;
145 * The end of the document.
147 private static final int END = -1;
150 * The character mapping in the document.
152 // FIXME: What is this used for?
153 private static final char[] charMapping = null;
156 * Set to true if one character has been read ahead.
158 private boolean didPushChar;
161 * The read ahead character.
163 private int pushedChar;
166 * Used to indicate blocks.
168 private int[] unitStack;
171 * Number of valid blocks.
173 private int stackCount;
176 * Holds the incoming CSS rules.
178 private Reader reader;
181 * Set to true when the first non @ rule is encountered.
183 private boolean encounteredRuleSet;
186 * The call back used to parse.
188 private CSSParser.CSSParserCallback callback;
191 * nextToken() inserts the string here.
193 private char[] tokenBuffer;
196 * Current number of chars in tokenBufferLength.
198 private int tokenBufferLength;
201 * Set to true if any whitespace is read.
203 private boolean readWS;
210 tokenBuffer = new char[10];
214 * Appends a character to the token buffer.
216 * @param c - the character to append
218 private void append(char c)
220 if (tokenBuffer.length >= tokenBufferLength)
222 char[] temp = new char[tokenBufferLength * 2];
223 if (tokenBuffer != null)
224 System.arraycopy(tokenBuffer, 0, temp, 0, tokenBufferLength);
226 temp[tokenBufferLength] = c;
230 tokenBuffer[tokenBufferLength] = c;
235 * Fetches the next token.
237 * @param c - the character to fetch.
238 * @return the location
239 * @throws IOException - any i/o error encountered while reading
241 private int nextToken(char c) throws IOException
249 if (tokenBufferLength > 0)
253 if (tokenBufferLength > 0)
267 return BRACKET_CLOSE;
278 * Reads a character from the stream.
280 * @return the number of characters read or -1 if end of stream is reached.
281 * @throws IOException - any i/o encountered while reading
283 private int readChar() throws IOException
290 return reader.read();
294 * Parses the the contents of the reader using the
297 * @param reader - the reader to read from
298 * @param callback - the callback instance
299 * @param parsingDeclaration - true if parsing a declaration
300 * @throws IOException - any i/o error from the reader
302 void parse(Reader reader, CSSParser.CSSParserCallback callback,
303 boolean parsingDeclaration)
306 this.reader = reader;
307 this.callback = callback;
311 if (!parsingDeclaration)
312 while(getNextStatement())
315 parseDeclarationBlock();
317 catch (IOException ioe)
319 // Nothing to do here.
324 * Skips any white space, returning the character after the white space.
326 * @return the character after the whitespace
327 * @throws IOException - any i/o error from the reader
329 private int readWS() throws IOException
331 int next = readChar();
332 while (Character.isWhitespace((char) next))
335 int tempNext = readChar();
341 // Its all whitespace
346 * Gets the next statement, returning false if the end is reached.
347 * A statement is either an At-rule, or a ruleset.
349 * @return false if the end is reached
350 * @throws IOException - any i/o error from the reader
352 private boolean getNextStatement() throws IOException
354 int c = nextToken((char) 0);
365 throw new IOException("Not a proper statement.");
367 if (tokenBuffer[0] == ('@'))
379 * Parses an @ rule, stopping at a matching brace pair, or ;.
381 * @throws IOException - any i/o error from the reader
383 private void parseAtRule() throws IOException
385 // An At-Rule begins with the "@" character followed immediately by a keyword.
386 // Following the keyword separated by a space is an At-rule statement appropriate
387 // to the At-keyword used. If the At-Rule is a simple declarative statement
388 // (charset, import, fontdef), it is terminated by a semi-colon (";".)
389 // If the At-Rule is a conditional or informative statement (media, page, font-face),
390 // it is followed by optional arguments and then a style declaration block inside matching
391 // curly braces ("{", "}".) At-Rules are sometimes nestable, depending on the context.
392 // If any part of an At-Rule is not understood, it should be ignored.
394 // FIXME: Not Implemented
399 * Parses the next rule set, which is a selector followed by a declaration
402 * @throws IOException - any i/o error from the reader
404 private void parseRuleSet() throws IOException
406 // call parseDeclarationBlock
407 // call parse selectors
408 // call parse identifiers
409 // call startrule/endrule
410 // FIXME: Not Implemented
414 * Parses a set of selectors, returning false if the end of the stream is
417 * @return false if the end of stream is reached
418 * @throws IOException - any i/o error from the reader
420 private boolean parseSelectors() throws IOException
422 // FIXME: Not Implemented
423 // call handleselector
428 * Parses a declaration block. Which a number of declarations followed by a
431 * @throws IOException - any i/o error from the reader
433 private void parseDeclarationBlock() throws IOException
435 // call parseDeclaration
436 // FIXME: Not Implemented
440 * Parses a single declaration, which is an identifier a : and another identifier.
441 * This returns the last token seen.
443 * @returns the last token
444 * @throws IOException - any i/o error from the reader
446 private int parseDeclaration() throws IOException
449 // FIXME: Not Implemented
454 * Parses identifiers until c is encountered, returning the ending token,
455 * which will be IDENTIFIER if c is found.
457 * @param c - the stop character
458 * @param wantsBlocks - true if blocks are wanted
459 * @return the ending token
460 * @throws IOException - any i/o error from the reader
462 private int parseIdentifiers(char c, boolean wantsBlocks) throws IOException
464 // FIXME: Not implemented
465 // call handleproperty?
470 * Parses till a matching block close is encountered. This is only appropriate
471 * to be called at the top level (no nesting).
474 * @throws IOException - any i/o error from the reader
476 private void parseTillClosed(int i) throws IOException
478 // FIXME: Not Implemented
482 * Gets an identifier, returning true if the length of the string is greater
483 * than 0, stopping when c, whitespace, or one of {}()[] is hit.
485 * @param c - the stop character
486 * @return returns true if the length of the string > 0
487 * @throws IOException - any i/o error from the reader
489 private boolean getIdentifier(char c) throws IOException
491 // FIXME: Not Implemented
496 * Reads till c is encountered, escaping characters as necessary.
498 * @param c - the stop character
499 * @throws IOException - any i/o error from the reader
501 private void readTill(char c) throws IOException
503 // FIXME: Not Implemented
507 * Parses a comment block.
509 * @throws IOException - any i/o error from the reader
511 private void readComment() throws IOException
513 // Should ignore comments. Read until end of comment.
514 // FIXME: Not implemented
518 * Called when a block start is encountered ({[.
520 * @param start of block
522 private void startBlock(int start)
524 // FIXME: Not Implemented
528 * Called when an end block is encountered )]}
530 * @param end of block
532 private void endBlock(int end)
534 // FIXME: Not Implemented
538 * Checks if currently in a block.
540 * @return true if currently in a block.
542 private boolean inBlock()
544 // FIXME: Not Implemented
549 * Supports one character look ahead, this will throw if called twice in a row.
551 * @param c - the character to push.
552 * @throws IOException - if called twice in a row
554 private void pushChar(int c) throws IOException
557 throw new IOException("pushChar called twice.");