1 11 package org.eclipse.core.internal.registry; 12 13 import java.io.PrintWriter ; 14 import org.eclipse.core.runtime.*; 15 16 20 public class RegistryWriter { 21 public static final int INDENT = 2; 22 public static final String REGISTRY = "plugin-registry"; 24 public RegistryWriter() { 25 super(); 26 } 27 28 public void writeConfigurationElement(ConfigurationElement configElement, PrintWriter w, int indent) { 29 String element = configElement.getName(); 30 if (element == null) 31 return; 32 33 String gap1 = ""; for (int i = 0; i < indent; i++) 35 gap1 += " "; String gap2 = gap1; 37 for (int i = 0; i < INDENT; i++) 38 gap2 += " "; 40 w.print(gap1 + "<" + element); 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("/>"); return; 51 } 52 w.println(">"); 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 + ">"); } 61 62 public void writeConfigurationProperty(ConfigurationProperty configProp, PrintWriter w) { 63 if (configProp.getName() == null) 64 return; 65 w.print(" " + xmlSafe(configProp.getName()) + "=\""); if (configProp.getValue() != null) 67 w.print(xmlSafe(configProp.getValue())); 68 w.print("\""); } 70 71 public void writeExtension(Extension extension, PrintWriter w, int indent) { 72 String gap1 = ""; for (int i = 0; i < indent; i++) 74 gap1 += " "; 76 w.print(gap1 + "<" + ExtensionsParser.EXTENSION); if (extension.getExtensionPointIdentifier() != null) 78 w.print(" " + ExtensionsParser.EXTENSION_TARGET + "=\"" + xmlSafe(extension.getExtensionPointIdentifier()) + "\""); if (extension.getUniqueIdentifier() != null) 80 w.print(" " + ExtensionsParser.EXTENSION_ID + "=\"" + xmlSafe(extension.getUniqueIdentifier()) + "\""); if (extension.getName() != null) 82 w.print(" " + ExtensionsParser.EXTENSION_NAME + "=\"" + xmlSafe(extension.getName()) + "\""); 84 IConfigurationElement[] subElements = extension.getConfigurationElements(); 85 int size = (subElements == null) ? 0 : subElements.length; 86 if (size == 0) { 87 w.println("/>"); return; 89 } 90 w.println(">"); for (int i = 0; i < size; i++) 92 writeConfigurationElement((ConfigurationElement) subElements[i], w, indent + INDENT); 93 94 w.println(gap1 + "</" + ExtensionsParser.EXTENSION + ">"); } 96 97 public void writeExtensionPoint(ExtensionPoint extPt, PrintWriter w, int indent) { 98 String gap1 = ""; for (int i = 0; i < indent; i++) 100 gap1 += " "; 102 w.print(gap1 + "<" + ExtensionsParser.EXTENSION_POINT); if (extPt.getUniqueIdentifier() != null) 104 w.print(" " + ExtensionsParser.EXTENSION_POINT_ID + "=\"" + xmlSafe(extPt.getUniqueIdentifier()) + "\""); if (extPt.getName() != null) 106 w.print(" " + ExtensionsParser.EXTENSION_POINT_NAME + "=\"" + xmlSafe(extPt.getName()) + "\""); w.println("/>"); } 109 110 public void writeBundleModel(Namespace plugin, PrintWriter w, int indent) { 111 112 String gap1 = ""; for (int i = 0; i < indent; i++) 114 gap1 += " "; String gap2 = gap1; 116 for (int i = 0; i < INDENT; i++) 117 gap2 += " "; 119 w.println(""); w.print(gap1 + "<" + ExtensionsParser.PLUGIN); if (plugin.getUniqueIdentifier() != null) 122 w.print(" " + ExtensionsParser.PLUGIN_ID + "=\"" + xmlSafe(plugin.getUniqueIdentifier()) + "\""); if (plugin.getName() != null) 124 w.print(" " + ExtensionsParser.PLUGIN_NAME + "=\"" + xmlSafe(plugin.getName()) + "\""); w.println(">"); 127 IExtensionPoint[] extensionPoints = plugin.getExtensionPoints(); 128 int extPointsSize = (extensionPoints == null) ? 0 : extensionPoints.length; 129 if (extPointsSize != 0) { 130 w.println(""); 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(""); writeExtension((Extension) extensions[i], w, indent + INDENT); 141 } 142 } 143 144 w.println(gap1 + "</" + ExtensionsParser.PLUGIN + ">"); } 149 150 public void writeRegistry(ExtensionRegistry registry, PrintWriter w, int indent) { 151 String gap1 = ""; for (int i = 0; i < indent; i++) 153 gap1 += " "; w.println(gap1 + "<" + REGISTRY + ">"); String [] 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 + ">"); w.flush(); 161 } 162 163 private static void appendEscapedChar(StringBuffer buffer, char c) { 164 String 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("&#"); buffer.append(Integer.toString(c)); 175 buffer.append(';'); 176 } 177 } 178 } 179 180 public static String xmlSafe(String s) { 181 StringBuffer result = new StringBuffer (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 getReplacement(char c) { 188 switch (c) { 191 case '<' : 192 return "lt"; case '>' : 194 return "gt"; case '"' : 196 return "quot"; case '\'' : 198 return "apos"; case '&' : 200 return "amp"; } 202 return null; 203 } 204 } | Popular Tags |