Source Code Cross Referenced for XmlHeapModel.java in  » IDE-Netbeans » library » org » netbeans » insane » model » 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 » IDE Netbeans » library » org.netbeans.insane.model 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         *
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         *
006:         * The contents of this file are subject to the terms of either the GNU
007:         * General Public License Version 2 only ("GPL") or the Common
008:         * Development and Distribution License("CDDL") (collectively, the
009:         * "License"). You may not use this file except in compliance with the
010:         * License. You can obtain a copy of the License at
011:         * http://www.netbeans.org/cddl-gplv2.html
012:         * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013:         * specific language governing permissions and limitations under the
014:         * License.  When distributing the software, include this License Header
015:         * Notice in each file and include the License file at
016:         * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
017:         * particular file as subject to the "Classpath" exception as provided
018:         * by Sun in the GPL Version 2 section of the License file that
019:         * accompanied this code. If applicable, add the following below the
020:         * License Header, with the fields enclosed by brackets [] replaced by
021:         * your own identifying information:
022:         * "Portions Copyrighted [year] [name of copyright owner]"
023:         *
024:         * Contributor(s):
025:         *
026:         * The Original Software is NetBeans. The Initial Developer of the Original
027:         * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028:         * Microsystems, Inc. All Rights Reserved.
029:         *
030:         * If you wish your version of this file to be governed by only the CDDL
031:         * or only the GPL Version 2, indicate your decision by adding
032:         * "[Contributor] elects to include this software in this distribution
033:         * under the [CDDL or GPL Version 2] license." If you do not indicate a
034:         * single choice of license, a recipient has the option to distribute
035:         * your version of this file under either the CDDL, the GPL Version 2 or
036:         * to extend the choice of license to its licensees as provided above.
037:         * However, if you add GPL Version 2 code and therefore, elected the GPL
038:         * Version 2 license, then the option applies only if the new code is
039:         * made subject to such option by the copyright holder.
040:         */
041:
042:        package org.netbeans.insane.model;
043:
044:        import java.io.*;
045:        import java.util.*;
046:        import javax.xml.parsers.SAXParser;
047:        import javax.xml.parsers.SAXParserFactory;
048:        import org.xml.sax.*;
049:        import org.xml.sax.helpers.DefaultHandler;
050:
051:        /**
052:         * An implementation of the heap model that parses SimpleXmlVisitor output
053:         * and keeps all the data in memory.
054:         *
055:         * @author  Nenik
056:         */
057:        class XmlHeapModel implements  org.netbeans.insane.model.HeapModel {
058:
059:            public static HeapModel parse(File file) throws Exception {
060:                HeapModel model = new XmlHeapModel(new InputSource(
061:                        new FileInputStream(file)));
062:                return model;
063:            }
064:
065:            private Map<Integer, Item> items = new HashMap<Integer, Item>();
066:            private Map<String, Item> roots = new HashMap<String, Item>();
067:
068:            // HeapModel interface implementation
069:            public Iterator<Item> getAllItems() {
070:                return Collections.unmodifiableCollection(items.values())
071:                        .iterator();
072:            }
073:
074:            public Collection<Item> getObjectsOfType(String type) {
075:                Collection<Item> filter = new ArrayList<Item>();
076:                for (Iterator<Item> it = getAllItems(); it.hasNext();) {
077:                    Item act = it.next();
078:                    if (type.equals(act.getType()))
079:                        filter.add(act);
080:                }
081:                return filter;
082:            }
083:
084:            public Collection<String> getRoots() {
085:                return Collections.unmodifiableSet(roots.keySet());
086:            }
087:
088:            public Item getObjectAt(String staticRefName) {
089:                return roots.get(staticRefName);
090:            }
091:
092:            public Item getItem(int id) {
093:                Item itm = items.get(new java.lang.Integer(id));
094:                if (itm == null)
095:                    throw new IllegalArgumentException("Bad ID");
096:                return itm;
097:            }
098:
099:            static class MemItem implements  Item {
100:                // a list of Items, Strings and one null
101:                private Object[] refs = new Object[] { null };
102:                private int id;
103:                private int size;
104:                private String type;
105:                private String value;
106:
107:                MemItem(int id, String type, int size, String value) {
108:                    this .id = id;
109:                    this .type = type.intern();
110:                    this .size = size;
111:                    this .value = value;
112:                }
113:
114:                // Item interface implementation    
115:                public String getType() {
116:                    return type;
117:                }
118:
119:                public int getSize() {
120:                    return size;
121:                }
122:
123:                public String getValue() {
124:                    return value;
125:                }
126:
127:                public Enumeration<Object> incomming() {
128:                    return new RefEnum(true, refs);
129:                }
130:
131:                public Enumeration<Item> outgoing() {
132:                    return new RefEnum(false, refs);
133:                }
134:
135:                public int getId() {
136:                    return id;
137:                }
138:
139:                // debug helper
140:                public String toString() {
141:                    if (value == null) {
142:                        return type + "@" + Integer.toHexString(id);
143:                    } else {
144:                        return type + "@" + Integer.toHexString(id) + ": \""
145:                                + value + '"';
146:                    }
147:                }
148:
149:                // parsing impl
150:                void addIncomming(Object incomming) {
151:                    Object[] nr = new Object[refs.length + 1];
152:                    nr[0] = incomming;
153:                    System.arraycopy(refs, 0, nr, 1, refs.length);
154:                    refs = nr;
155:                }
156:
157:                void addOutgoing(Object outgoing) {
158:                    Object[] nr = new Object[refs.length + 1];
159:                    nr[refs.length] = outgoing;
160:                    System.arraycopy(refs, 0, nr, 0, refs.length);
161:                    refs = nr;
162:                }
163:            }
164:
165:            //parser implementation
166:
167:            private XmlHeapModel(InputSource is) throws Exception {
168:                Handler h = new Handler();
169:                SAXParserFactory fact = SAXParserFactory.newInstance();
170:                SAXParser parser = fact.newSAXParser();
171:                parser.getXMLReader().setContentHandler(h);
172:                parser.getXMLReader().parse(is);
173:            }
174:
175:            Item createItem(int id, String type, int size, String val) {
176:                Item item = new MemItem(id, type, size, val);
177:                items.put(new Integer(id), item);
178:                return item;
179:            }
180:
181:            void addReference(int from, int to) {
182:                MemItem f = (MemItem) getItem(from);
183:                MemItem t = (MemItem) getItem(to);
184:                f.addOutgoing(t);
185:                t.addIncomming(f);
186:            }
187:
188:            void addReference(String stat, int to) {
189:                MemItem t = (MemItem) getItem(to);
190:                t.addIncomming(stat);
191:            }
192:
193:            private class Handler extends DefaultHandler {
194:                private int depth = 0;
195:
196:                public void startElement(String namespaceURI, String localName,
197:                        String qName, Attributes atts) throws SAXException {
198:                    if (depth == 0) {
199:                        if (!"insane".equals(qName))
200:                            throw new SAXException("format");
201:                    } else if (depth != 1) {
202:                        throw new SAXException("format");
203:                    } else {
204:                        if ("object".equals(qName)) {
205:                            String id = atts.getValue("id");
206:                            String type = atts.getValue("type");
207:                            String size = atts.getValue("size");
208:                            String val = atts.getValue("value");
209:                            createItem(getIdFromString(id), type, Integer
210:                                    .parseInt(size), val);
211:                        } else if ("ref".equals(qName)) {
212:                            String from = atts.getValue("from");
213:                            String name = atts.getValue("name");
214:                            String to = atts.getValue("to");
215:                            //                        if (! "java.lang.ref.Reference.referent".equals(name)) {
216:                            if (from != null) {
217:                                addReference(getIdFromString(from),
218:                                        getIdFromString(to));
219:                            } else {
220:                                addReference(name, getIdFromString(to));
221:                            }
222:                            //                        }
223:                        } else {
224:                            throw new SAXException("format");
225:                        }
226:                    }
227:                    depth++;
228:                }
229:
230:                public void endElement(String namespaceURI, String localName,
231:                        String qName) throws SAXException {
232:                    depth--;
233:                }
234:            }
235:
236:            int getIdFromString(String s) {
237:                return Integer.parseInt(s, 16);
238:            }
239:
240:            // An enumeration over object array, enumeration either pre-null items
241:            // or post-null items
242:            private static class RefEnum implements  Enumeration {
243:                int ptr;
244:                Object[] items;
245:
246:                RefEnum(boolean first, Object[] data) {
247:                    items = data;
248:                    if (!first)
249:                        while (data[ptr++] != null)
250:                            ;
251:                }
252:
253:                public boolean hasMoreElements() {
254:                    return ptr < items.length && items[ptr] != null;
255:                }
256:
257:                public Object nextElement() {
258:                    if (hasMoreElements())
259:                        return items[ptr++];
260:                    throw new NoSuchElementException();
261:                }
262:            }
263:
264:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.