1 11 package org.eclipse.help.internal.dynamic; 12 13 import org.eclipse.help.internal.Anchor; 14 import org.eclipse.help.internal.UAElement; 15 import org.eclipse.help.internal.UAElementFactory; 16 import org.eclipse.help.internal.extension.ContentExtension; 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.Node ; 19 20 24 public class ExtensionHandler extends ProcessorHandler { 25 26 private static final String ATTRIBUTE_ID = "id"; 28 private ExtensionResolver resolver; 29 private DocumentReader reader; 30 private String locale; 31 32 36 public ExtensionHandler(DocumentReader reader, String locale) { 37 this.reader = reader; 38 this.locale = locale; 39 } 40 41 public short handle(UAElement element, String path) { 42 if (element instanceof Anchor) { 43 return handleExtension(element, path, ContentExtension.CONTRIBUTION); 44 } 45 if (element.getAttribute(ATTRIBUTE_ID) != null) { 46 return handleExtension(element, path, ContentExtension.REPLACEMENT); 47 } 48 return UNHANDLED; 49 } 50 51 55 private short handleExtension(UAElement uaElement, String path, int type) { 56 String id = uaElement.getAttribute(ATTRIBUTE_ID); 57 if (id != null && id.length() > 0) { 58 if (resolver == null) { 59 resolver = new ExtensionResolver(getProcessor(), reader, locale); 60 } 61 Node [] nodes = resolver.resolveExtension(path + '#' + id, type); 63 if (nodes != null && nodes.length > 0) { 64 Element domElement = uaElement.element; 65 UAElement parent = uaElement.getParentElement(); 66 for (int i=0;i<nodes.length;++i) { 67 if (nodes[i].getNodeType() == Node.ELEMENT_NODE) { 68 parent.insertBefore(UAElementFactory.newElement((Element)nodes[i]), uaElement); 70 } 71 else { 72 Node node = domElement.getOwnerDocument().importNode(nodes[i], true); 74 parent.element.insertBefore(node, domElement); 75 } 76 } 77 parent.removeChild(uaElement); 78 return HANDLED_SKIP; 79 } 80 } 81 if (type == ContentExtension.CONTRIBUTION) { 83 uaElement.getParentElement().removeChild(uaElement); 84 return HANDLED_SKIP; 85 } 86 return UNHANDLED; 88 } 89 } 90 | Popular Tags |