KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > extension > ContentExtensionFileParser


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.extension;
12
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21
22 import org.eclipse.help.IUAElement;
23 import org.eclipse.help.internal.UAElement;
24 import org.eclipse.help.internal.dynamic.DocumentProcessor;
25 import org.eclipse.help.internal.dynamic.DocumentReader;
26 import org.eclipse.help.internal.dynamic.ProcessorHandler;
27 import org.eclipse.help.internal.dynamic.ValidationHandler;
28 import org.osgi.framework.Bundle;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 /*
33  * Parses content extension XML files into extension model elements.
34  */

35 public class ContentExtensionFileParser extends DefaultHandler JavaDoc {
36
37     private DocumentReader reader;
38     private DocumentProcessor processor;
39     private Map JavaDoc requiredAttributes;
40     private Map JavaDoc deprecatedElements;
41     
42     /*
43      * Parses the specified content extension XML file into model elements.
44      */

45     public ContentExtension[] parse(Bundle bundle, String JavaDoc path) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
46         if (reader == null) {
47             reader = new DocumentReader();
48         }
49         URL JavaDoc url = bundle.getEntry(path);
50         if (url != null) {
51             InputStream JavaDoc in = url.openStream();
52             UAElement extension = (UAElement)reader.read(in);
53             if (processor == null) {
54                 processor = new DocumentProcessor(new ProcessorHandler[] {
55                     new ValidationHandler(getRequiredAttributes(), getDeprecatedElements())
56                 });
57             }
58             processor.process(extension, '/' + bundle.getSymbolicName() + '/' + path);
59             IUAElement[] children = extension.getChildren();
60             ContentExtension[] result = new ContentExtension[children.length];
61             System.arraycopy(children, 0, result, 0, children.length);
62             return result;
63         }
64         else {
65             throw new FileNotFoundException JavaDoc();
66         }
67     }
68
69     private Map JavaDoc getRequiredAttributes() {
70         if (requiredAttributes == null) {
71             requiredAttributes = new HashMap JavaDoc();
72             requiredAttributes.put(ContentExtension.NAME_CONTRIBUTION, new String JavaDoc[] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH });
73             requiredAttributes.put(ContentExtension.NAME_CONTRIBUTION_LEGACY, new String JavaDoc[] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH });
74             requiredAttributes.put(ContentExtension.NAME_REPLACEMENT, new String JavaDoc[] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH });
75             requiredAttributes.put(ContentExtension.NAME_REPLACEMENT_LEGACY, new String JavaDoc[] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH });
76         }
77         return requiredAttributes;
78     }
79
80     private Map JavaDoc getDeprecatedElements() {
81         if (deprecatedElements == null) {
82             deprecatedElements = new HashMap JavaDoc();
83             deprecatedElements.put(ContentExtension.NAME_CONTRIBUTION_LEGACY, ContentExtension.NAME_CONTRIBUTION);
84             deprecatedElements.put(ContentExtension.NAME_REPLACEMENT_LEGACY, ContentExtension.NAME_REPLACEMENT);
85         }
86         return deprecatedElements;
87     }
88 }
89
Popular Tags