Source Code Cross Referenced for TestEventHandler.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » beans » tests » support » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.beans.tests.support 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.