001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.nls;
011:
012: import java.util.Properties;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.core.plugin.IFragmentModel;
016: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
017: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
018: import org.eclipse.pde.internal.core.text.bundle.ManifestHeader;
019: import org.eclipse.pde.internal.core.text.plugin.PluginAttribute;
020: import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
021: import org.eclipse.pde.internal.core.text.plugin.PluginExtensionPointNode;
022:
023: public class ModelChangeElement {
024:
025: private static final String DELIM = "."; //$NON-NLS-1$
026: private static final String KEY_PREFIX = "%"; //$NON-NLS-1$
027: private static final String FRAGMENT_PREFIX = "f"; //$NON-NLS-1$
028:
029: private String fValue = ""; //$NON-NLS-1$
030: private String fKey = ""; //$NON-NLS-1$
031: private int fOffset = 0;
032: private int fLength = 0;
033: private boolean fExternalized = true;
034: private ModelChange fParent;
035: private Object fUnderlying;
036:
037: public ModelChangeElement(ModelChange parent, Object incoming) {
038: fParent = parent;
039: fUnderlying = incoming;
040: if (incoming instanceof PluginElementNode) {
041: PluginElementNode elem = (PluginElementNode) incoming;
042: IDocumentTextNode text = elem.getTextNode();
043: fValue = elem.getText();
044: generateValidKey(elem.getParent().getName(), elem.getName());
045: fOffset = text.getOffset();
046: fLength = text.getLength();
047: } else if (incoming instanceof PluginAttribute) {
048: PluginAttribute attr = (PluginAttribute) incoming;
049: fValue = attr.getValue();
050: generateValidKey(
051: attr.getEnclosingElement().getXMLTagName(), attr
052: .getName());
053: fOffset = attr.getValueOffset();
054: fLength = attr.getValueLength();
055: } else if (incoming instanceof PluginExtensionPointNode) {
056: PluginExtensionPointNode extP = (PluginExtensionPointNode) incoming;
057: fValue = extP.getName();
058: generateValidKey("extension-point", "name"); //$NON-NLS-1$ //$NON-NLS-2$
059: IDocumentAttributeNode attr = extP
060: .getDocumentAttribute("name"); //$NON-NLS-1$
061: fOffset = attr.getValueOffset();
062: fLength = attr.getValueLength();
063: } else if (incoming instanceof ManifestHeader) {
064: ManifestHeader header = (ManifestHeader) incoming;
065: fValue = header.getValue();
066: generateValidKey(header.getName());
067: fLength = fValue.length();
068: fOffset = header.getOffset() + header.getLength()
069: - header.getLineLimiter().length() - fLength;
070: }
071: }
072:
073: public String getKey() {
074: return fKey;
075: }
076:
077: public void setKey(String key) {
078: fKey = key;
079: }
080:
081: public String getValue() {
082: return fValue;
083: }
084:
085: public void setValue(String value) {
086: fValue = value;
087: }
088:
089: public boolean isExternalized() {
090: return fExternalized;
091: }
092:
093: public void setExternalized(boolean externalzied) {
094: fExternalized = externalzied;
095: }
096:
097: public int getOffset() {
098: return fOffset;
099: }
100:
101: public int getLength() {
102: return fLength;
103: }
104:
105: private void generateValidKey(String pre, String mid) {
106: generateValidKey(pre + DELIM + mid);
107: }
108:
109: private void generateValidKey(String key) {
110: int suffix = 0;
111: Properties properties = fParent.getProperties();
112: String newKey = fParent.getParentModel() instanceof IFragmentModel ? key
113: + DELIM + FRAGMENT_PREFIX
114: : key + DELIM;
115: while (properties.containsKey(newKey + suffix))
116: suffix += 1;
117: properties.setProperty(newKey + suffix, fValue);
118: fKey = newKey + suffix;
119: }
120:
121: public String getExternKey() {
122: return KEY_PREFIX + fKey;
123: }
124:
125: public boolean updateValue() {
126: try {
127: String key = getExternKey();
128: if (fUnderlying instanceof PluginElementNode) {
129: PluginElementNode elem = (PluginElementNode) fUnderlying;
130: elem.setText(key);
131: } else if (fUnderlying instanceof PluginAttribute) {
132: PluginAttribute attr = (PluginAttribute) fUnderlying;
133: String attrName = attr.getName();
134: attr.getEnclosingElement().setXMLAttribute(attrName,
135: key);
136: } else if (fUnderlying instanceof PluginExtensionPointNode) {
137: PluginExtensionPointNode extP = (PluginExtensionPointNode) fUnderlying;
138: extP.setName(key);
139: } else if (fUnderlying instanceof ManifestHeader) {
140: ManifestHeader header = (ManifestHeader) fUnderlying;
141: header.setValue(key);
142: } else
143: return false;
144: } catch (CoreException e) {
145: return false;
146: }
147: return true;
148: }
149: }
|