KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import javax.xml.parsers.ParserConfigurationException JavaDoc;
17
18 import org.eclipse.help.HelpSystem;
19 import org.eclipse.help.IUAElement;
20 import org.eclipse.help.internal.UAElement;
21 import org.xml.sax.SAXException JavaDoc;
22
23 /*
24  * Resolves includes by parsing the target document, extracting the node and
25  * replacing the original include with it.
26  */

27 public class IncludeResolver {
28     
29     private static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
30

31     private DocumentProcessor processor;
32     private DocumentReader reader;
33     private String JavaDoc locale;
34     
35     public IncludeResolver(DocumentProcessor processor, DocumentReader reader, String JavaDoc locale) {
36         this.processor = processor;
37         this.reader = reader;
38         this.locale = locale;
39     }
40     
41     /*
42      * Resolves the include target to a processed Element.
43      */

44     public UAElement resolve(String JavaDoc bundleId, String JavaDoc relativePath, String JavaDoc elementId) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
45         String JavaDoc href = '/' + bundleId + '/' + relativePath;
46         InputStream JavaDoc in = HelpSystem.getHelpContent(href, locale);
47         try {
48             UAElement element = findElement(in, elementId);
49             processor.process(element, href);
50             return element;
51         }
52         finally {
53             try {
54                 in.close();
55             }
56             catch (IOException JavaDoc e) {}
57         }
58     }
59     
60     /*
61      * Finds the specified element from the given XML input stream.
62      */

63     private UAElement findElement(InputStream JavaDoc in, String JavaDoc elementId) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
64         UAElement element = reader.read(in);
65         return findElement(element, elementId);
66     }
67     
68     /*
69      * Finds the specified element under the given subtree.
70      */

71     private UAElement findElement(UAElement element, String JavaDoc elementId) {
72         String JavaDoc id = element.getAttribute(ATTRIBUTE_ID);
73         if (id != null && id.equals(elementId)) {
74             return element;
75         }
76         IUAElement[] children = element.getChildren();
77         for (int i=0;i<children.length;++i) {
78             UAElement result = findElement((UAElement)children[i], elementId);
79             if (result != null) {
80                 return result;
81             }
82         }
83         return null;
84     }
85 }
86
Popular Tags