KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > UAElement


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

11 package org.eclipse.help.internal;
12
13 import java.io.StringReader JavaDoc;
14 import java.lang.reflect.Array JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18 import javax.xml.parsers.DocumentBuilder JavaDoc;
19 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21
22 import org.eclipse.core.expressions.EvaluationResult;
23 import org.eclipse.core.expressions.Expression;
24 import org.eclipse.core.expressions.ExpressionConverter;
25 import org.eclipse.core.expressions.ExpressionTagNames;
26 import org.eclipse.core.expressions.IEvaluationContext;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.help.HelpSystem;
29 import org.eclipse.help.IUAElement;
30 import org.eclipse.help.internal.dynamic.FilterResolver;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.xml.sax.EntityResolver JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /*
39  * Base class for UA model elements.
40  */

41 public class UAElement implements IUAElement {
42
43     private static final String JavaDoc ELEMENT_FILTER = "filter"; //$NON-NLS-1$
44
private static final String JavaDoc ATTRIBUTE_FILTER = "filter"; //$NON-NLS-1$
45
private static final String JavaDoc ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
46
private static final String JavaDoc ATTRIBUTE_VALUE = "value"; //$NON-NLS-1$
47

48     private static DocumentBuilder JavaDoc builder;
49     private static Document JavaDoc document;
50     
51     public Element element;
52     public IUAElement src;
53     public UAElement parent;
54     public UAElement[] children; // cache
55

56     public UAElement(Element element) {
57         this.element = element;
58     }
59
60     public UAElement(String JavaDoc name) {
61         this.element = getDocument().createElement(name);
62     }
63     
64     public UAElement(String JavaDoc name, IUAElement src) {
65         this(name);
66         this.src = src;
67     }
68     
69     public void appendChild(UAElement uaElementToAppend) {
70         importElement(uaElementToAppend);
71         element.appendChild(uaElementToAppend.element);
72         uaElementToAppend.parent = this;
73         
74         // cache is now invalid
75
children = null;
76     }
77
78     public void appendChildren(IUAElement[] children) {
79         for (int i=0;i<children.length;i++) {
80             appendChild(children[i] instanceof UAElement ? (UAElement)children[i] : UAElementFactory.newElement(children[i]));
81         }
82     }
83     
84     public String JavaDoc getAttribute(String JavaDoc name) {
85         String JavaDoc value = element.getAttribute(name);
86         if (value.length() > 0) {
87             return value;
88         }
89         return null;
90     }
91
92     public IUAElement[] getChildren() {
93         if (children == null) {
94             if (element.hasChildNodes()) {
95                 List JavaDoc list = new ArrayList JavaDoc();
96                 Node JavaDoc node = element.getFirstChild();
97                 while (node != null) {
98                     if (node.getNodeType() == Node.ELEMENT_NODE) {
99                         UAElement uaElement = UAElementFactory.newElement((Element)node);
100                         uaElement.parent = this;
101                         if (uaElement != null) {
102                             list.add(uaElement);
103                         }
104                     }
105                     node = node.getNextSibling();
106                 }
107                 children = (UAElement[])list.toArray(new UAElement[list.size()]);
108             }
109             else {
110                 children = new UAElement[0];
111             }
112         }
113         return children;
114     }
115     
116     public Object JavaDoc getChildren(Class JavaDoc clazz) {
117         IUAElement[] children = getChildren();
118         if (children.length > 0) {
119             List JavaDoc list = new ArrayList JavaDoc();
120             for (int i=0;i<children.length;++i) {
121                 IUAElement child = children[i];
122                 if (clazz.isAssignableFrom(child.getClass())) {
123                     list.add(child);
124                 }
125             }
126             return list.toArray((Object JavaDoc[])Array.newInstance(clazz, list.size()));
127         }
128         return Array.newInstance(clazz, 0);
129     }
130     
131     public String JavaDoc getElementName() {
132         return element.getNodeName();
133     }
134     
135     public static Document JavaDoc getDocument() {
136         if (document == null) {
137             if (builder == null) {
138                 try {
139                     builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
140                     builder.setEntityResolver(new EntityResolver JavaDoc() {
141                         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
142                             return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
143
}
144                     });
145                 }
146                 catch (ParserConfigurationException JavaDoc e) {
147                     String JavaDoc msg = "Error creating document builder"; //$NON-NLS-1$
148
HelpPlugin.logError(msg, e);
149                 }
150             }
151             document = builder.newDocument();
152         }
153         return document;
154     }
155
156     public UAElement getParentElement() {
157         return parent;
158     }
159
160     public void insertBefore(UAElement newChild, UAElement refChild) {
161         importElement(newChild);
162         element.insertBefore(newChild.element, refChild.element);
163         newChild.parent = this;
164
165         // cache is now invalid
166
children = null;
167     }
168     
169     public boolean isEnabled(IEvaluationContext context) {
170         if (HelpSystem.isShared()) {
171             return true;
172         }
173         if (src != null) {
174             return src.isEnabled(context);
175         }
176         String JavaDoc filter = getAttribute(ATTRIBUTE_FILTER);
177         if (filter != null) {
178             return isEnabledByFilterAttribute(filter);
179         }
180         Node JavaDoc node = element.getFirstChild();
181         while (node != null) {
182             if (node.getNodeType() == Node.ELEMENT_NODE) {
183                 String JavaDoc name = node.getNodeName();
184                 if (ExpressionTagNames.ENABLEMENT.equals(name)) {
185                     return isEnabledByEnablementElement((Element)node, context);
186                 }
187                 else if (ELEMENT_FILTER.equals(name)) {
188                     // can be multiple filter elements; enabled if they all pass
189
if (!isEnabledByFilterElement((Element)node)) {
190                         return false;
191                     }
192                 }
193             }
194             node = node.getNextSibling();
195         }
196         return true;
197     }
198     
199     public void removeChild(UAElement elementToRemove) {
200         element.removeChild(elementToRemove.element);
201         elementToRemove.parent = null;
202
203         // cache is now invalid
204
children = null;
205     }
206     
207     public void setAttribute(String JavaDoc name, String JavaDoc value) {
208         element.setAttribute(name, value);
209     }
210
211     private void importElement(UAElement uaElementToImport) {
212         Element elementToImport = uaElementToImport.element;
213         Document JavaDoc ownerDocument = element.getOwnerDocument();
214         if (!ownerDocument.equals(elementToImport.getOwnerDocument())) {
215             elementToImport = (Element)ownerDocument.importNode(elementToImport, true);
216         }
217         uaElementToImport.element = elementToImport;
218     }
219
220     private boolean isEnabledByEnablementElement(Element enablement, IEvaluationContext context) {
221         try {
222             Expression expression = ExpressionConverter.getDefault().perform(enablement);
223             return expression.evaluate(context) == EvaluationResult.TRUE;
224         }
225         catch (CoreException e) {
226             /*
227              * This can happen when attempting to resolve a UI variable (e.g. "workbench")
228              * in a non-UI environment (infocenter mode). Fail silently.
229              */

230             return true;
231         }
232     }
233     
234     private boolean isEnabledByFilterAttribute(String JavaDoc filter) {
235         return !FilterResolver.getInstance().isFiltered(filter);
236     }
237     
238     private boolean isEnabledByFilterElement(Element filter) {
239         String JavaDoc name = filter.getAttribute(ATTRIBUTE_NAME);
240         String JavaDoc value = filter.getAttribute(ATTRIBUTE_VALUE);
241         if (name.length() > 0 && value.length() > 0) {
242             boolean not = false;
243             if (value.startsWith("!")) { //$NON-NLS-1$
244
not = true;
245                 value = value.substring(1);
246             }
247             return !FilterResolver.getInstance().isFiltered(name, value, not);
248         }
249         // ignore invalid filters
250
return true;
251     }
252 }
253
Popular Tags