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.scanner;
043:
044: import java.io.*;
045: import java.lang.reflect.Field;
046:
047: /**
048: * A visitor that stores the heap graph to a XML file in a simple format,
049: * which was used by the original Insane implementation.
050: *
051: * Usage pattern:
052: * <pre>
053: * SimpleXmlVisitor visitor = new SimpleXmlVisitor(new File("/tmp/insane.xml"));
054: * ScannerUtils.scan(null, visitor, rotset, true);
055: * visitor.close();
056: * </pre>
057: *
058: * @author Nenik
059: */
060: public final class SimpleXmlVisitor implements Visitor {
061: private static char[] pom = new char[0];
062: private static Class CHAR_ARRAY = pom.getClass();
063:
064: private Writer writer;
065: private IOException storedException;
066:
067: /** Creates a new instance of SimpleXmlVisitor */
068: public SimpleXmlVisitor(File to) throws IOException {
069: writer = new OutputStreamWriter(new FileOutputStream(to));
070: writer.write("<insane>\n");
071: }
072:
073: public void close() throws IOException {
074: writer.write("</insane>\n");
075: writer.close();
076: if (storedException != null)
077: throw storedException;
078: }
079:
080: // ignore for this xml format
081: public void visitClass(Class cls) {
082: }
083:
084: public void visitObject(ObjectMap map, Object obj) {
085: try {
086: if (CHAR_ARRAY == obj.getClass()) {
087: char[] copy = ((char[]) obj).clone();
088: for (int i = 0; i < copy.length; i++) {
089: if (copy[i] < 0x20)
090: copy[i] = '.';
091: if (copy[i] >= 0x80)
092: copy[i] = '.';
093: if (copy[i] == '\'')
094: copy[i] = '"';
095: if (copy[i] == '<')
096: copy[i] = '_';
097: if (copy[i] == '&')
098: copy[i] = '_';
099: }
100:
101: writer.write("<object id='" + map.getID(obj)
102: + "' type='" + obj.getClass().getName()
103: + "' size='" + ScannerUtils.sizeOf(obj)
104: + "' value='" + new String(copy) + "'/>\n");
105: } else {
106: writer.write("<object id='" + map.getID(obj)
107: + "' type='" + obj.getClass().getName()
108: + "' size='" + ScannerUtils.sizeOf(obj)
109: + "'/>\n");
110: }
111: } catch (IOException ioe) {
112: storedException = ioe;
113: }
114: }
115:
116: public void visitObjectReference(ObjectMap map, Object from,
117: Object to, Field ref) {
118: try {
119: writer.write("<ref from='" + map.getID(from) + "' name='"
120: + getFldName(ref) + "' to='" + map.getID(to)
121: + "'/>\n");
122: } catch (IOException ioe) {
123: storedException = ioe;
124: }
125: }
126:
127: public void visitStaticReference(ObjectMap map, Object to, Field ref) {
128: try {
129: writer.write("<ref name='" + getFldName(ref) + "' to='"
130: + map.getID(to) + "'/>\n");
131: } catch (IOException ioe) {
132: storedException = ioe;
133: }
134: }
135:
136: public void visitArrayReference(ObjectMap map, Object from,
137: Object to, int index) {
138: try {
139: writer.write("<ref from='" + map.getID(from) + "' name='"
140: + index + "' to='" + map.getID(to) + "'/>\n");
141: } catch (IOException ioe) {
142: storedException = ioe;
143: }
144: }
145:
146: private static String getFldName(Field fld) {
147: return fld.getDeclaringClass().getName() + "." + fld.getName();
148: }
149:
150: }
|