1 11 package org.eclipse.help.internal.extension; 12 13 import java.io.FileNotFoundException ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.net.URL ; 17 import java.util.HashMap ; 18 import java.util.Map ; 19 20 import javax.xml.parsers.ParserConfigurationException ; 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 ; 30 import org.xml.sax.helpers.DefaultHandler ; 31 32 35 public class ContentExtensionFileParser extends DefaultHandler { 36 37 private DocumentReader reader; 38 private DocumentProcessor processor; 39 private Map requiredAttributes; 40 private Map deprecatedElements; 41 42 45 public ContentExtension[] parse(Bundle bundle, String path) throws IOException , SAXException , ParserConfigurationException { 46 if (reader == null) { 47 reader = new DocumentReader(); 48 } 49 URL url = bundle.getEntry(path); 50 if (url != null) { 51 InputStream 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 (); 66 } 67 } 68 69 private Map getRequiredAttributes() { 70 if (requiredAttributes == null) { 71 requiredAttributes = new HashMap (); 72 requiredAttributes.put(ContentExtension.NAME_CONTRIBUTION, new String [] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH }); 73 requiredAttributes.put(ContentExtension.NAME_CONTRIBUTION_LEGACY, new String [] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH }); 74 requiredAttributes.put(ContentExtension.NAME_REPLACEMENT, new String [] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH }); 75 requiredAttributes.put(ContentExtension.NAME_REPLACEMENT_LEGACY, new String [] { ContentExtension.ATTRIBUTE_CONTENT, ContentExtension.ATTRIBUTE_PATH }); 76 } 77 return requiredAttributes; 78 } 79 80 private Map getDeprecatedElements() { 81 if (deprecatedElements == null) { 82 deprecatedElements = new HashMap (); 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 |