KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > RegistryWriter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.registry;
12
13 import java.io.PrintWriter JavaDoc;
14 import org.eclipse.core.runtime.*;
15
16 /**
17  * The RegistryWriter is a helper/debugging class that dumps a loaded registry
18  * in a reasonably human readable form (i.e., XML).
19  */

20 public class RegistryWriter {
21     public static final int INDENT = 2;
22     public static final String JavaDoc REGISTRY = "plugin-registry"; //$NON-NLS-1$
23

24     public RegistryWriter() {
25         super();
26     }
27
28     public void writeConfigurationElement(ConfigurationElement configElement, PrintWriter JavaDoc w, int indent) {
29         String JavaDoc element = configElement.getName();
30         if (element == null)
31             return;
32
33         String JavaDoc gap1 = ""; //$NON-NLS-1$
34
for (int i = 0; i < indent; i++)
35             gap1 += " "; //$NON-NLS-1$
36
String JavaDoc gap2 = gap1;
37         for (int i = 0; i < INDENT; i++)
38             gap2 += " "; //$NON-NLS-1$
39

40         w.print(gap1 + "<" + element); //$NON-NLS-1$
41
ConfigurationProperty[] propList = configElement.getProperties();
42         int propSize = (propList == null) ? 0 : propList.length;
43         for (int i = 0; i < propSize; i++)
44             writeConfigurationProperty(propList[i], w);
45
46         IConfigurationElement[] subElementList = configElement.getChildren();
47         int subElementSize = (subElementList == null) ? 0 : subElementList.length;
48         if (configElement.getValue() == null && subElementSize == 0) {
49             w.println("/>"); //$NON-NLS-1$
50
return;
51         }
52         w.println(">"); //$NON-NLS-1$
53

54         if (configElement.getValue() != null)
55             w.println(gap2 + xmlSafe(configElement.getValue()));
56         for (int i = 0; i < subElementSize; i++)
57             writeConfigurationElement((ConfigurationElement) subElementList[i], w, indent + INDENT);
58
59         w.println(gap1 + "</" + element + ">"); //$NON-NLS-1$ //$NON-NLS-2$
60
}
61
62     public void writeConfigurationProperty(ConfigurationProperty configProp, PrintWriter JavaDoc w) {
63         if (configProp.getName() == null)
64             return;
65         w.print(" " + xmlSafe(configProp.getName()) + "=\""); //$NON-NLS-1$ //$NON-NLS-2$
66
if (configProp.getValue() != null)
67             w.print(xmlSafe(configProp.getValue()));
68         w.print("\""); //$NON-NLS-1$
69
}
70
71     public void writeExtension(Extension extension, PrintWriter JavaDoc w, int indent) {
72         String JavaDoc gap1 = ""; //$NON-NLS-1$
73
for (int i = 0; i < indent; i++)
74             gap1 += " "; //$NON-NLS-1$
75

76         w.print(gap1 + "<" + ExtensionsParser.EXTENSION); //$NON-NLS-1$
77
if (extension.getExtensionPointIdentifier() != null)
78             w.print(" " + ExtensionsParser.EXTENSION_TARGET + "=\"" + xmlSafe(extension.getExtensionPointIdentifier()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
79
if (extension.getUniqueIdentifier() != null)
80             w.print(" " + ExtensionsParser.EXTENSION_ID + "=\"" + xmlSafe(extension.getUniqueIdentifier()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
81
if (extension.getName() != null)
82             w.print(" " + ExtensionsParser.EXTENSION_NAME + "=\"" + xmlSafe(extension.getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
83

84         IConfigurationElement[] subElements = extension.getConfigurationElements();
85         int size = (subElements == null) ? 0 : subElements.length;
86         if (size == 0) {
87             w.println("/>"); //$NON-NLS-1$
88
return;
89         }
90         w.println(">"); //$NON-NLS-1$
91
for (int i = 0; i < size; i++)
92             writeConfigurationElement((ConfigurationElement) subElements[i], w, indent + INDENT);
93
94         w.println(gap1 + "</" + ExtensionsParser.EXTENSION + ">"); //$NON-NLS-1$ //$NON-NLS-2$
95
}
96
97     public void writeExtensionPoint(ExtensionPoint extPt, PrintWriter JavaDoc w, int indent) {
98         String JavaDoc gap1 = ""; //$NON-NLS-1$
99
for (int i = 0; i < indent; i++)
100             gap1 += " "; //$NON-NLS-1$
101

102         w.print(gap1 + "<" + ExtensionsParser.EXTENSION_POINT); //$NON-NLS-1$
103
if (extPt.getUniqueIdentifier() != null)
104             w.print(" " + ExtensionsParser.EXTENSION_POINT_ID + "=\"" + xmlSafe(extPt.getUniqueIdentifier()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
105
if (extPt.getName() != null)
106             w.print(" " + ExtensionsParser.EXTENSION_POINT_NAME + "=\"" + xmlSafe(extPt.getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
107
w.println("/>"); //$NON-NLS-1$
108
}
109
110     public void writeBundleModel(Namespace plugin, PrintWriter JavaDoc w, int indent) {
111
112         String JavaDoc gap1 = ""; //$NON-NLS-1$
113
for (int i = 0; i < indent; i++)
114             gap1 += " "; //$NON-NLS-1$
115
String JavaDoc gap2 = gap1;
116         for (int i = 0; i < INDENT; i++)
117             gap2 += " "; //$NON-NLS-1$
118

119         w.println(""); //$NON-NLS-1$
120
w.print(gap1 + "<" + ExtensionsParser.PLUGIN); //$NON-NLS-1$
121
if (plugin.getUniqueIdentifier() != null)
122             w.print(" " + ExtensionsParser.PLUGIN_ID + "=\"" + xmlSafe(plugin.getUniqueIdentifier()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
123
if (plugin.getName() != null)
124             w.print(" " + ExtensionsParser.PLUGIN_NAME + "=\"" + xmlSafe(plugin.getName()) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
125
w.println(">"); //$NON-NLS-1$
126

127         IExtensionPoint[] extensionPoints = plugin.getExtensionPoints();
128         int extPointsSize = (extensionPoints == null) ? 0 : extensionPoints.length;
129         if (extPointsSize != 0) {
130             w.println(""); //$NON-NLS-1$
131
for (int i = 0; i < extPointsSize; i++)
132                 writeExtensionPoint((ExtensionPoint) extensionPoints[i], w, indent + INDENT);
133         }
134
135         IExtension[] extensions = plugin.getExtensions();
136         int extSize = (extensions == null) ? 0 : extensions.length;
137         if (extSize != 0) {
138             for (int i = 0; i < extSize; i++) {
139                 w.println(""); //$NON-NLS-1$
140
writeExtension((Extension) extensions[i], w, indent + INDENT);
141             }
142         }
143
144         // Don't write fragments here. If we do, XML won't be
145
// able to parse what we write out. Fragments must be
146
// entities separate from plugins.
147
w.println(gap1 + "</" + ExtensionsParser.PLUGIN + ">"); //$NON-NLS-1$ //$NON-NLS-2$
148
}
149
150     public void writeRegistry(ExtensionRegistry registry, PrintWriter JavaDoc w, int indent) {
151         String JavaDoc gap1 = ""; //$NON-NLS-1$
152
for (int i = 0; i < indent; i++)
153             gap1 += " "; //$NON-NLS-1$
154
w.println(gap1 + "<" + REGISTRY + ">"); //$NON-NLS-1$ //$NON-NLS-2$
155
String JavaDoc[] list = registry.getNamespaces();
156         for (int i = 0; i < list.length; i++)
157             writeBundleModel(registry.getNamespace(list[i]), w, indent + INDENT);
158
159         w.println(gap1 + "</" + REGISTRY + ">"); //$NON-NLS-1$ //$NON-NLS-2$
160
w.flush();
161     }
162
163     private static void appendEscapedChar(StringBuffer JavaDoc buffer, char c) {
164         String JavaDoc replacement = getReplacement(c);
165         if (replacement != null) {
166             buffer.append('&');
167             buffer.append(replacement);
168             buffer.append(';');
169         } else {
170             if ((c >= ' ' && c <= 0x7E) || c == '\n' || c == '\r' || c == '\t') {
171                 buffer.append(c);
172             } else {
173                 buffer.append("&#"); //$NON-NLS-1$
174
buffer.append(Integer.toString(c));
175                 buffer.append(';');
176             }
177         }
178     }
179
180     public static String JavaDoc xmlSafe(String JavaDoc s) {
181         StringBuffer JavaDoc result = new StringBuffer JavaDoc(s.length() + 10);
182         for (int i = 0; i < s.length(); ++i)
183             appendEscapedChar(result, s.charAt(i));
184         return result.toString();
185     }
186
187     private static String JavaDoc getReplacement(char c) {
188         // Encode special XML characters into the equivalent character references.
189
// These five are defined by default for all XML documents.
190
switch (c) {
191             case '<' :
192                 return "lt"; //$NON-NLS-1$
193
case '>' :
194                 return "gt"; //$NON-NLS-1$
195
case '"' :
196                 return "quot"; //$NON-NLS-1$
197
case '\'' :
198                 return "apos"; //$NON-NLS-1$
199
case '&' :
200                 return "amp"; //$NON-NLS-1$
201
}
202         return null;
203     }
204 }
Popular Tags