001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /*
019: * Created on 27.06.2006
020: * @author Alexei Y. Zakharov
021: */
022: package org.apache.harmony.beans.tests.support;
023:
024: import java.io.FileReader;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.LinkedHashMap;
029: import java.util.LinkedHashSet;
030: import java.util.Map;
031:
032: import org.xml.sax.Attributes;
033: import org.xml.sax.InputSource;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.DefaultHandler;
037: import org.xml.sax.helpers.XMLReaderFactory;
038:
039: /**
040: * This class is used by XMLEncoderTest for handling SAX parser events.
041: *
042: * @author Alexei Zakharov
043: */
044: public class TestEventHandler extends DefaultHandler {
045:
046: public Tag root = null;
047:
048: private Tag currentTag = null;
049:
050: @Override
051: public void startDocument() {
052: }
053:
054: @Override
055: public void endDocument() {
056: }
057:
058: @Override
059: public void startElement(String uri, String name, String qName,
060: Attributes atts) {
061: Tag theTag = new Tag(name, currentTag);
062:
063: theTag.fillAttributes(atts);
064: if (currentTag != null) {
065: currentTag.innerTags.add(theTag);
066: }
067: if (root == null) {
068: root = theTag;
069: }
070: currentTag = theTag;
071: }
072:
073: @Override
074: public void endElement(String uri, String name, String qName)
075: throws SAXException {
076: if (!name.equals(currentTag.name)) {
077: throw new SAXException("unexpected closing tag: " + name);
078: }
079: currentTag = currentTag.parent;
080: }
081:
082: @Override
083: public void characters(char ch[], int start, int length) {
084: currentTag.content.append(ch, start, length);
085: }
086:
087: public static void main(String argv[]) throws Exception {
088:
089: XMLReader xmlReader;
090: TestEventHandler handler = new TestEventHandler();
091: FileReader reader;
092: String saxParserClassName = System
093: .getProperty("org.xml.sax.driver");
094:
095: if (saxParserClassName == null) {
096: saxParserClassName = "org.apache.xerces.parsers.SAXParser";
097: }
098: xmlReader = XMLReaderFactory
099: .createXMLReader(saxParserClassName);
100: xmlReader.setContentHandler(handler);
101: xmlReader.setErrorHandler(handler);
102:
103: if (argv.length < 1) {
104: throw new Exception("input file should be specified");
105: }
106: reader = new FileReader(argv[0]);
107: xmlReader.parse(new InputSource(reader));
108: System.out.println(handler.root.toString());
109: }
110:
111: static class Tag {
112: String name;
113:
114: HashMap<String, String> attributes = new LinkedHashMap<String, String>();
115:
116: HashSet<Tag> innerTags = new LinkedHashSet<Tag>();
117:
118: Tag parent = null;
119:
120: StringBuffer content = new StringBuffer();
121:
122: boolean ignoreJavaVersion = true;
123:
124: public Tag(String name, Tag parent) {
125: this .name = name;
126: this .parent = parent;
127: }
128:
129: public void fillAttributes(Attributes attrs) {
130: for (int i = 0; i < attrs.getLength(); i++) {
131: String name = attrs.getLocalName(i);
132: String value = attrs.getValue(i);
133:
134: attributes.put(name, value);
135: }
136: }
137:
138: @Override
139: public boolean equals(Object obj) {
140: Iterator<Map.Entry<String, String>> it;
141: Iterator<Tag> itTag;
142: Tag tag;
143:
144: if (!(obj instanceof Tag)) {
145: return false;
146: }
147: tag = (Tag) obj;
148:
149: // name
150: if (!name.equals(tag.name)) {
151: return false;
152: }
153:
154: // attributes
155: if (attributes.entrySet().size() != tag.attributes
156: .entrySet().size()) {
157: return false;
158: }
159: it = attributes.entrySet().iterator();
160:
161: while (it.hasNext()) {
162: Map.Entry<String, String> entry = it.next();
163: Iterator<Map.Entry<String, String>> it2 = tag.attributes
164: .entrySet().iterator();
165: boolean found = false;
166:
167: while (it2.hasNext()) {
168: Map.Entry<String, String> entry2 = it2.next();
169:
170: if (entry2.getKey().equals(entry.getKey())) {
171: if (ignoreJavaVersion
172: && tag.name.equals("java")
173: && entry.getKey().equals("version")) {
174: // ignore java version
175: found = true;
176: break;
177: } else if (entry2.getValue().equals(
178: entry.getValue())) {
179: // values are the same
180: found = true;
181: break;
182: }
183: }
184: }
185: if (!found) {
186: return false;
187: }
188: }
189:
190: // inner tags
191: if (innerTags.size() != tag.innerTags.size()) {
192: return false;
193: }
194: itTag = innerTags.iterator();
195: while (itTag.hasNext()) {
196: Tag innerTag = itTag.next();
197: Iterator<Tag> itTag2 = tag.innerTags.iterator();
198: boolean found = false;
199:
200: while (itTag2.hasNext()) {
201: Tag innerTag2 = itTag2.next();
202:
203: if (innerTag.equals(innerTag2)) {
204: found = true;
205: break;
206: }
207: }
208: if (!found) {
209: return false;
210: }
211: }
212: return true;
213: }
214:
215: @Override
216: public String toString() {
217: StringBuffer sb = new StringBuffer();
218: Iterator<Map.Entry<String, String>> it;
219:
220: sb.append('<' + name);
221: it = attributes.entrySet().iterator();
222: while (it.hasNext()) {
223: Map.Entry<String, String> entry = it.next();
224:
225: sb.append(" " + entry.getKey() + "=\""
226: + entry.getValue() + "\"");
227: }
228: if (innerTags.isEmpty() && content.length() == 0) {
229: sb.append("/>\n");
230: } else if (innerTags.isEmpty() && content.length() > 0) {
231: sb.append(">");
232: sb.append(content);
233: sb.append("</" + name + ">\n");
234: } else {
235: Iterator<Tag> it2 = innerTags.iterator();
236:
237: sb.append(">\n");
238: while (it2.hasNext()) {
239: Tag child = it2.next();
240:
241: sb.append(child.toString() + "\n");
242: }
243: sb.append("</" + name + ">\n");
244: }
245: return sb.toString();
246: }
247: }
248:
249: }
|