KickJava   Java API By Example, From Geeks To Geeks.

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


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.Include;
14 import org.eclipse.help.internal.UAElement;
15
16 /*
17  * The handler responsible for processing includes, where a node is pulled
18  * in from another document.
19  */

20 public class IncludeHandler extends ProcessorHandler {
21
22     private IncludeResolver resolver;
23     private DocumentReader reader;
24     private String JavaDoc locale;
25     
26     /*
27      * Creates the handler. It needs to know which locale the current document
28      * is in in order to pull content from the correct locale.
29      */

30     public IncludeHandler(DocumentReader reader, String JavaDoc locale) {
31         this.reader = reader;
32         this.locale = locale;
33     }
34     
35     public short handle(UAElement element, String JavaDoc id) {
36         if (element instanceof Include) {
37             String JavaDoc path = ((Include)element).getPath();
38             if (path != null && path.length() > 0) {
39                 String JavaDoc bundleId = getBundleId(path);
40                 String JavaDoc relativePath = getRelativePath(path);
41                 String JavaDoc elementId = getElementId(path);
42                 if (bundleId != null && relativePath != null && elementId != null) {
43                     resolveInclude(bundleId, relativePath, elementId, element, locale);
44                 }
45             }
46             else {
47                 // remove invalid includes
48
element.getParentElement().removeChild(element);
49             }
50             return HANDLED_SKIP;
51         }
52         return UNHANDLED;
53     }
54     
55     /*
56      * Processes the include; replaces the element with the one described by
57      * the parameters.
58      */

59     private void resolveInclude(String JavaDoc bundleId, String JavaDoc relativePath, String JavaDoc elementId, UAElement element, String JavaDoc locale) {
60         if (resolver == null) {
61             resolver = new IncludeResolver(getProcessor(), reader, locale);
62         }
63         UAElement parent = element.getParentElement();
64         if (parent != null) {
65             try {
66                 UAElement nodeToInclude = resolver.resolve(bundleId, relativePath, elementId);
67                 parent.insertBefore(nodeToInclude, element);
68                 parent.removeChild(element);
69             }
70             catch (Throwable JavaDoc t) {
71                 // remove invalid includes
72
parent.removeChild(element);
73             }
74         }
75     }
76     
77     /*
78      * Extracts the bundle ID from the given path.
79      */

80     private String JavaDoc getBundleId(String JavaDoc path) {
81         if (path.charAt(0) == '/') {
82             int index = path.indexOf('/', 1);
83             if (index > 1) {
84                 return path.substring(1, index);
85             }
86         }
87         else {
88             // legacy - handle no slash at beginning
89
int index = path.indexOf('/');
90             if (index != -1) {
91                 return path.substring(0, index);
92             }
93         }
94         return null;
95     }
96     
97     /*
98      * Extracts the bundle-relative path from the given full path.
99      */

100     private String JavaDoc getRelativePath(String JavaDoc path) {
101         int startIndex = path.indexOf('/', 1);
102         int endIndex = path.indexOf('#');
103         if (endIndex == -1) {
104             // legacy - can use slash in place of '#'
105
endIndex = path.lastIndexOf('/');
106         }
107         if (startIndex != -1 && endIndex > startIndex + 1) {
108             return path.substring(startIndex + 1, endIndex);
109         }
110         return null;
111     }
112     
113     /*
114      * Extracts the element id from the given path.
115      */

116     private String JavaDoc getElementId(String JavaDoc path) {
117         int index = path.indexOf('#');
118         if (index == -1) {
119             // legacy - can use slash in place of '#'
120
index = path.lastIndexOf('/');
121         }
122         if (index != -1 && index < path.length() - 1) {
123             return path.substring(index + 1);
124         }
125         return null;
126     }
127 }
128
Popular Tags