001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.core.text.plugin;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.core.IModelChangedEvent;
016: import org.eclipse.pde.core.plugin.IPluginLibrary;
017: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
018: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
019:
020: public class PluginLibraryNode extends PluginObjectNode implements
021: IPluginLibrary {
022:
023: private static final long serialVersionUID = 1L;
024:
025: /* (non-Javadoc)
026: * @see org.eclipse.pde.core.plugin.IPluginLibrary#getContentFilters()
027: */
028: public String[] getContentFilters() {
029: IDocumentElementNode[] children = getChildNodes();
030: ArrayList result = new ArrayList();
031: for (int i = 0; i < children.length; i++) {
032: PluginObjectNode node = (PluginObjectNode) children[i];
033: if (node.getName().equals(P_EXPORTED)) {
034: String name = children[i].getXMLAttributeValue(P_NAME);
035: if (name != null && !name.equals("*")) { //$NON-NLS-1$
036: int index = name.indexOf(".*"); //$NON-NLS-1$
037: if (index != -1)
038: name = name.substring(0, index);
039: result.add(name);
040: }
041: }
042: }
043: return (String[]) result.toArray(new String[result.size()]);
044: }
045:
046: /* (non-Javadoc)
047: * @see org.eclipse.pde.core.plugin.IPluginLibrary#getPackages()
048: */
049: public String[] getPackages() {
050: return new String[0];
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.pde.core.plugin.IPluginLibrary#isExported()
055: */
056: public boolean isExported() {
057: IDocumentElementNode[] children = getChildNodes();
058: for (int i = 0; i < children.length; i++) {
059: PluginObjectNode node = (PluginObjectNode) children[i];
060: if (node.getName().equals(P_EXPORTED))
061: return true;
062: }
063: return false;
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.pde.core.plugin.IPluginLibrary#isFullyExported()
068: */
069: public boolean isFullyExported() {
070: IDocumentElementNode[] children = getChildNodes();
071: for (int i = 0; i < children.length; i++) {
072: PluginObjectNode node = (PluginObjectNode) children[i];
073: if (node.getName().equals(P_EXPORTED)) {
074: String name = children[i].getXMLAttributeValue(P_NAME);
075: if (name != null && name.equals("*")) //$NON-NLS-1$
076: return true;
077: }
078: }
079: return false;
080: }
081:
082: /* (non-Javadoc)
083: * @see org.eclipse.pde.core.plugin.IPluginLibrary#getType()
084: */
085: public String getType() {
086: String type = getXMLAttributeValue(P_TYPE);
087: return (type != null && type.equals("resource")) ? IPluginLibrary.RESOURCE : IPluginLibrary.CODE; //$NON-NLS-1$
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.pde.core.plugin.IPluginLibrary#setContentFilters(java.lang.String[])
092: */
093: public void setContentFilters(String[] filters)
094: throws CoreException {
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.pde.core.plugin.IPluginLibrary#addContentFilter(java.lang.String)
099: */
100: public void addContentFilter(String filter) throws CoreException {
101: PluginElementNode node = new PluginElementNode();
102: node.setXMLTagName(P_EXPORTED);
103: node.setParentNode(this );
104: node.setModel(getModel());
105: node
106: .setXMLAttribute(
107: P_NAME,
108: "*".equals(filter) || filter.endsWith(".*") ? filter : filter + ".*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
109: addContentFilter(node);
110: }
111:
112: public void addContentFilter(PluginElementNode node)
113: throws CoreException {
114: addChildNode(node);
115: if (isInTheModel()) {
116: node.setInTheModel(true);
117: fireStructureChanged(node, IModelChangedEvent.INSERT);
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.pde.core.plugin.IPluginLibrary#removeContentFilter(java.lang.String)
123: */
124: public void removeContentFilter(String filter) throws CoreException {
125: if (!filter.endsWith(".*")) //$NON-NLS-1$
126: filter += ".*"; //$NON-NLS-1$
127: IDocumentElementNode[] children = getChildNodes();
128: for (int i = 0; i < children.length; i++) {
129: if (children[i].getXMLTagName().equals(P_EXPORTED)
130: && filter.equals(children[i]
131: .getXMLAttributeValue(P_NAME))) {
132: removeContentFilter((PluginElementNode) children[i]);
133: }
134: }
135: }
136:
137: public void removeContentFilter(PluginElementNode node) {
138: removeChildNode(node);
139: if (isInTheModel()) {
140: node.setInTheModel(false);
141: fireStructureChanged(node, IModelChangedEvent.REMOVE);
142: }
143: }
144:
145: /* (non-Javadoc)
146: * @see org.eclipse.pde.core.plugin.IPluginLibrary#setPackages(java.lang.String[])
147: */
148: public void setPackages(String[] packages) throws CoreException {
149: }
150:
151: /* (non-Javadoc)
152: * @see org.eclipse.pde.core.plugin.IPluginLibrary#setExported(boolean)
153: */
154: public void setExported(boolean exported) throws CoreException {
155: IDocumentElementNode[] children = getChildNodes();
156: boolean alreadyExported = false;
157: for (int i = 0; i < children.length; i++) {
158: if (children[i].getXMLTagName().equals(P_EXPORTED)) {
159: if (!"*".equals(children[i].getXMLAttributeValue(P_NAME))) { //$NON-NLS-1$
160: removeContentFilter((PluginElementNode) children[i]);
161: } else {
162: alreadyExported = true;
163: if (!exported) {
164: removeContentFilter((PluginElementNode) children[i]);
165: }
166: }
167: }
168: }
169: if (exported && !alreadyExported) {
170: addContentFilter("*"); //$NON-NLS-1$
171: }
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.pde.core.plugin.IPluginLibrary#setType(java.lang.String)
176: */
177: public void setType(String type) throws CoreException {
178: }
179:
180: /* (non-Javadoc)
181: * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
182: */
183: public String getName() {
184: return getXMLAttributeValue(P_NAME);
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
189: */
190: public void setName(String name) throws CoreException {
191: setXMLAttribute(P_NAME, name);
192: }
193:
194: /* (non-Javadoc)
195: * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
196: */
197: public String write(boolean indent) {
198: String sep = getLineDelimiter();
199: StringBuffer buffer = new StringBuffer();
200: if (indent)
201: buffer.append(getIndent());
202:
203: IDocumentElementNode[] children = getChildNodes();
204: if (children.length > 0) {
205: buffer.append(writeShallow(false) + sep);
206: for (int i = 0; i < children.length; i++) {
207: children[i].setLineIndent(getLineIndent() + 3);
208: buffer.append(children[i].write(true) + sep);
209: }
210: buffer.append(getIndent() + "</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
211: } else {
212: buffer.append(writeShallow(true));
213: }
214: return buffer.toString();
215: }
216:
217: /* (non-Javadoc)
218: * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow(boolean)
219: */
220: public String writeShallow(boolean terminate) {
221: StringBuffer buffer = new StringBuffer("<" + getXMLTagName()); //$NON-NLS-1$
222:
223: IDocumentAttributeNode[] attrs = getNodeAttributes();
224: for (int i = 0; i < attrs.length; i++) {
225: appendAttribute(buffer, attrs[i].getAttributeName());
226: }
227: if (terminate)
228: buffer.append("/"); //$NON-NLS-1$
229: buffer.append(">"); //$NON-NLS-1$
230: return buffer.toString();
231: }
232:
233: public String toString() {
234: return getName();
235: }
236:
237: }
|