1 11 package org.eclipse.pde.internal.ui.nls; 12 13 import java.util.Properties ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.pde.core.plugin.IFragmentModel; 17 import org.eclipse.pde.internal.core.text.IDocumentAttribute; 18 import org.eclipse.pde.internal.core.text.IDocumentTextNode; 19 import org.eclipse.pde.internal.core.text.bundle.ManifestHeader; 20 import org.eclipse.pde.internal.core.text.plugin.PluginAttribute; 21 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode; 22 import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode; 23 24 public class ModelChangeElement { 25 26 private static final String DELIM = "."; private static final String KEY_PREFIX = "%"; private static final String FRAGMENT_PREFIX = "f"; 30 private String fValue = ""; private String fKey = ""; private int fOffset = 0; 33 private int fLength = 0; 34 private boolean fExternalized = true; 35 private ModelChange fParent; 36 private Object fUnderlying; 37 38 public ModelChangeElement(ModelChange parent, Object incoming) { 39 fParent = parent; 40 fUnderlying = incoming; 41 if (incoming instanceof PluginElementNode) { 42 PluginElementNode elem = (PluginElementNode)incoming; 43 IDocumentTextNode text = elem.getTextNode(); 44 fValue = elem.getText(); 45 generateValidKey(elem.getParent().getName(), elem.getName()); 46 fOffset = text.getOffset(); 47 fLength = text.getLength(); 48 } else if (incoming instanceof PluginAttribute) { 49 PluginAttribute attr = (PluginAttribute)incoming; 50 fValue = attr.getValue(); 51 generateValidKey(attr.getEnclosingElement().getXMLTagName(), attr.getName()); 52 fOffset = attr.getValueOffset(); 53 fLength = attr.getValueLength(); 54 } else if (incoming instanceof PluginExtensionPointNode) { 55 PluginExtensionPointNode extP = (PluginExtensionPointNode)incoming; 56 fValue = extP.getName(); 57 generateValidKey("extension-point", "name"); IDocumentAttribute attr = extP.getDocumentAttribute("name"); fOffset = attr.getValueOffset(); 60 fLength = attr.getValueLength(); 61 } else if (incoming instanceof ManifestHeader) { 62 ManifestHeader header = (ManifestHeader)incoming; 63 fValue = header.getValue(); 64 generateValidKey(header.getName()); 65 fLength = fValue.length(); 66 fOffset = header.getOffset() + header.getLength() - header.getLineLimiter().length() - fLength; 67 } 68 } 69 70 public String getKey() { 71 return fKey; 72 } 73 public void setKey(String key) { 74 fKey = key; 75 } 76 public String getValue() { 77 return fValue; 78 } 79 public void setValue(String value) { 80 fValue = value; 81 } 82 public boolean isExternalized() { 83 return fExternalized; 84 } 85 public void setExternalized(boolean externalzied) { 86 fExternalized = externalzied; 87 } 88 public int getOffset() { 89 return fOffset; 90 } 91 public int getLength() { 92 return fLength; 93 } 94 95 private void generateValidKey(String pre, String mid) { 96 generateValidKey(pre + DELIM + mid); 97 } 98 99 private void generateValidKey(String key) { 100 int suffix = 0; 101 Properties properties = fParent.getProperties(); 102 String newKey = fParent.getParentModel() instanceof IFragmentModel ? 103 key + DELIM + FRAGMENT_PREFIX : 104 key + DELIM; 105 while (properties.containsKey(newKey + suffix)) 106 suffix += 1; 107 properties.setProperty(newKey + suffix, fValue); 108 fKey = newKey + suffix; 109 } 110 public String getExternKey() { 111 return KEY_PREFIX + fKey; 112 } 113 114 public boolean updateValue() { 115 try { 116 String key = getExternKey(); 117 if (fUnderlying instanceof PluginElementNode) { 118 PluginElementNode elem = (PluginElementNode)fUnderlying; 119 elem.setText(key); 120 } else if (fUnderlying instanceof PluginAttribute) { 121 PluginAttribute attr = (PluginAttribute)fUnderlying; 122 String attrName = attr.getName(); 123 attr.getEnclosingElement().setXMLAttribute(attrName, key); 124 } else if (fUnderlying instanceof PluginExtensionPointNode) { 125 PluginExtensionPointNode extP = (PluginExtensionPointNode)fUnderlying; 126 extP.setName(key); 127 } else if (fUnderlying instanceof ManifestHeader) { 128 ManifestHeader header = (ManifestHeader)fUnderlying; 129 header.setValue(key); 130 } else 131 return false; 132 } catch (CoreException e) { 133 return false; 134 } 135 return true; 136 } 137 } 138 | Popular Tags |