KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > dynamic > ExtensionHandler


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.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 JavaDoc;
18 import org.w3c.dom.Node JavaDoc;
19
20 /*
21  * The handler responsible for processing content extensions (contributions
22  * into anchors and element replacements).
23  */

24 public class ExtensionHandler extends ProcessorHandler {
25
26     private static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
27

28     private ExtensionResolver resolver;
29     private DocumentReader reader;
30     private String JavaDoc locale;
31     
32     /*
33      * This handler must know the locale since it's pulling content
34      * in from other documents.
35      */

36     public ExtensionHandler(DocumentReader reader, String JavaDoc locale) {
37         this.reader = reader;
38         this.locale = locale;
39     }
40     
41     public short handle(UAElement element, String JavaDoc 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     /*
52      * Handle the given extension-related node. It is either an anchor
53      * or an element with an id that could potentially be replaced.
54      */

55     private short handleExtension(UAElement uaElement, String JavaDoc path, int type) {
56         String JavaDoc 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             // get the nodes to insert/replace with
62
Node JavaDoc[] 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                         // ensure elements are typed
69
parent.insertBefore(UAElementFactory.newElement((Element)nodes[i]), uaElement);
70                     }
71                     else {
72                         // text nodes are not typed
73
Node JavaDoc 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         // always remove anchors, even invalid ones
82
if (type == ContentExtension.CONTRIBUTION) {
83             uaElement.getParentElement().removeChild(uaElement);
84             return HANDLED_SKIP;
85         }
86         // it was an element with id, but no replacements
87
return UNHANDLED;
88     }
89 }
90
Popular Tags