001: // Copyright 2006 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal.services;
016:
017: import java.io.BufferedInputStream;
018: import java.net.URL;
019:
020: import javax.xml.parsers.SAXParser;
021: import javax.xml.parsers.SAXParserFactory;
022:
023: import org.xml.sax.Attributes;
024: import org.xml.sax.InputSource;
025: import org.xml.sax.Locator;
026: import org.xml.sax.SAXException;
027: import org.xml.sax.helpers.DefaultHandler;
028:
029: /** Used to experiment with namespace aware SAX parsers. */
030: public class ParserExperiment extends DefaultHandler {
031: public static void main(String[] args) throws Exception {
032: new ParserExperiment().parse("basic.html");
033: }
034:
035: public void parse(String file) throws Exception {
036: parse(getClass().getResource(file));
037: }
038:
039: public void parse(URL document) throws Exception {
040: SAXParserFactory factory = SAXParserFactory.newInstance();
041:
042: factory.setNamespaceAware(true);
043: // Equivalent:
044: // factory.setFeature("http://xml.org/sax/features/namespaces", true);
045:
046: // Doesn't seem to do anything:
047: factory
048: .setFeature(
049: "http://apache.org/xml/features/validation/schema/normalized-value",
050: true);
051:
052: // Doesn't seem to do anything:
053: factory.setFeature(
054: "http://xml.org/sax/features/namespace-prefixes", true);
055:
056: // A non-validation parser is fine!
057:
058: SAXParser parser = factory.newSAXParser();
059:
060: InputSource source = new InputSource(new BufferedInputStream(
061: document.openStream()));
062:
063: parser.parse(source, this );
064: }
065:
066: private void log(String methodName, String... details) {
067: StringBuilder buffer = new StringBuilder();
068:
069: buffer.append(String.format("%-25s:", methodName));
070:
071: if (_locator != null) {
072: buffer.append(String.format(" [Line %d, column %d]",
073: _locator.getLineNumber(), _locator
074: .getColumnNumber()));
075: }
076:
077: for (int i = 0; i < details.length; i++) {
078: buffer.append("\n ");
079: buffer.append(details[i]);
080: }
081:
082: System.out.println(buffer.toString());
083: }
084:
085: @Override
086: public void characters(char[] ch, int start, int length)
087: throws SAXException {
088: String string = new String(ch, start, length);
089: String loggable = string.replaceAll("![\\w -]", ".").trim();
090:
091: log("characters", "start=" + start, "length=" + length,
092: loggable);
093: }
094:
095: @Override
096: public void endDocument() throws SAXException {
097: log("endDocument");
098: }
099:
100: @Override
101: public void endElement(String uri, String localName, String qName)
102: throws SAXException {
103: log("endElement", localName, "uri=" + uri, "qName=" + qName);
104: }
105:
106: @Override
107: public void endPrefixMapping(String prefix) throws SAXException {
108: log("endPrefixMapping", prefix);
109: }
110:
111: @Override
112: public void ignorableWhitespace(char[] ch, int start, int length)
113: throws SAXException {
114: log("ignorableWhitespace", "start=" + start, "length=" + length);
115: }
116:
117: @Override
118: public void notationDecl(String name, String publicId,
119: String systemId) throws SAXException {
120: log("notationDecl", name, "publicId=" + publicId, "systemId="
121: + systemId);
122: }
123:
124: @Override
125: public void processingInstruction(String target, String data)
126: throws SAXException {
127: log("pi", "target=" + target, "data=" + data);
128: }
129:
130: private Locator _locator;
131:
132: @Override
133: public void setDocumentLocator(Locator locator) {
134: _locator = locator;
135:
136: log("setDocumentLocator", "publicId=" + locator.getPublicId(),
137: "systemId=" + locator.getSystemId());
138: }
139:
140: @Override
141: public void skippedEntity(String name) throws SAXException {
142: log("skippedEntity", name);
143: }
144:
145: @Override
146: public void startDocument() throws SAXException {
147: log("startDocument");
148: }
149:
150: @Override
151: public void startElement(String uri, String localName,
152: String qName, Attributes attributes) throws SAXException {
153: log("startElement", localName, "uri=" + uri, "qName=" + qName);
154:
155: int count = attributes.getLength();
156:
157: for (int i = 0; i < count; i++) {
158: log("attribute", attributes.getLocalName(i), "value="
159: + attributes.getValue(i), "qName="
160: + attributes.getQName(i));
161: }
162: }
163:
164: @Override
165: public void startPrefixMapping(String prefix, String uri)
166: throws SAXException {
167: log("startPrefixMapping", "prefix=" + prefix, "uri=" + uri);
168: }
169:
170: @Override
171: public void unparsedEntityDecl(String name, String publicId,
172: String systemId, String notationName) throws SAXException {
173: log("unparsedEntityDecl", name, "publicId=" + publicId,
174: "systemId=" + systemId);
175: }
176:
177: }
|