1 11 package org.eclipse.help.internal; 12 13 import java.io.StringReader ; 14 import java.lang.reflect.Array ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 18 import javax.xml.parsers.DocumentBuilder ; 19 import javax.xml.parsers.DocumentBuilderFactory ; 20 import javax.xml.parsers.ParserConfigurationException ; 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 ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.Node ; 34 import org.xml.sax.EntityResolver ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 41 public class UAElement implements IUAElement { 42 43 private static final String ELEMENT_FILTER = "filter"; private static final String ATTRIBUTE_FILTER = "filter"; private static final String ATTRIBUTE_NAME = "name"; private static final String ATTRIBUTE_VALUE = "value"; 48 private static DocumentBuilder builder; 49 private static Document document; 50 51 public Element element; 52 public IUAElement src; 53 public UAElement parent; 54 public UAElement[] children; 56 public UAElement(Element element) { 57 this.element = element; 58 } 59 60 public UAElement(String name) { 61 this.element = getDocument().createElement(name); 62 } 63 64 public UAElement(String 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 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 getAttribute(String name) { 85 String 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 list = new ArrayList (); 96 Node 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 getChildren(Class clazz) { 117 IUAElement[] children = getChildren(); 118 if (children.length > 0) { 119 List list = new ArrayList (); 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 [])Array.newInstance(clazz, list.size())); 127 } 128 return Array.newInstance(clazz, 0); 129 } 130 131 public String getElementName() { 132 return element.getNodeName(); 133 } 134 135 public static Document getDocument() { 136 if (document == null) { 137 if (builder == null) { 138 try { 139 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 140 builder.setEntityResolver(new EntityResolver () { 141 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 142 return new InputSource (new StringReader ("")); } 144 }); 145 } 146 catch (ParserConfigurationException e) { 147 String msg = "Error creating document builder"; 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 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 filter = getAttribute(ATTRIBUTE_FILTER); 177 if (filter != null) { 178 return isEnabledByFilterAttribute(filter); 179 } 180 Node node = element.getFirstChild(); 181 while (node != null) { 182 if (node.getNodeType() == Node.ELEMENT_NODE) { 183 String name = node.getNodeName(); 184 if (ExpressionTagNames.ENABLEMENT.equals(name)) { 185 return isEnabledByEnablementElement((Element)node, context); 186 } 187 else if (ELEMENT_FILTER.equals(name)) { 188 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 children = null; 205 } 206 207 public void setAttribute(String name, String value) { 208 element.setAttribute(name, value); 209 } 210 211 private void importElement(UAElement uaElementToImport) { 212 Element elementToImport = uaElementToImport.element; 213 Document 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 230 return true; 231 } 232 } 233 234 private boolean isEnabledByFilterAttribute(String filter) { 235 return !FilterResolver.getInstance().isFiltered(filter); 236 } 237 238 private boolean isEnabledByFilterElement(Element filter) { 239 String name = filter.getAttribute(ATTRIBUTE_NAME); 240 String value = filter.getAttribute(ATTRIBUTE_VALUE); 241 if (name.length() > 0 && value.length() > 0) { 242 boolean not = false; 243 if (value.startsWith("!")) { not = true; 245 value = value.substring(1); 246 } 247 return !FilterResolver.getInstance().isFiltered(name, value, not); 248 } 249 return true; 251 } 252 } 253 | Popular Tags |