1 11 package org.eclipse.core.runtime.content; 12 13 import java.io.*; 14 import java.util.Hashtable ; 15 import javax.xml.parsers.ParserConfigurationException ; 16 import org.eclipse.core.internal.content.*; 17 import org.eclipse.core.internal.runtime.RuntimeLog; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.xml.sax.InputSource ; 21 import org.xml.sax.SAXException ; 22 23 42 public final class XMLRootElementContentDescriber extends XMLContentDescriber implements IExecutableExtension { 43 private static final String DTD_TO_FIND = "dtd"; private static final String ELEMENT_TO_FIND = "element"; 51 private String dtdToFind = null; 52 58 private String elementToFind = null; 59 60 71 private int checkCriteria(InputSource contents) throws IOException { 72 XMLRootHandler xmlHandler = new XMLRootHandler(elementToFind != null); 73 try { 74 if (!xmlHandler.parseContents(contents)) 75 return INDETERMINATE; 76 } catch (SAXException e) { 77 return INDETERMINATE; 79 } catch (ParserConfigurationException e) { 80 String message = ContentMessages.content_parserConfiguration; 82 RuntimeLog.log(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, e)); 83 throw new RuntimeException (message); 84 } 85 if ((elementToFind != null) && (!elementToFind.equals(xmlHandler.getRootName()))) 87 return INDETERMINATE; 88 if ((dtdToFind != null) && (!dtdToFind.equals(xmlHandler.getDTD()))) 89 return INDETERMINATE; 90 return VALID; 92 } 93 94 97 public int describe(InputStream contents, IContentDescription description) throws IOException { 98 if (super.describe(contents, description) == INVALID) 100 return INVALID; 101 contents.reset(); 103 return checkCriteria(new InputSource (contents)); 105 } 106 107 110 public int describe(Reader contents, IContentDescription description) throws IOException { 111 if (super.describe(contents, description) == INVALID) 113 return INVALID; 114 contents.reset(); 116 return checkCriteria(new InputSource (contents)); 118 } 119 120 123 public void setInitializationData(final IConfigurationElement config, final String propertyName, final Object data) throws CoreException { 124 if (data instanceof String ) 125 elementToFind = (String ) data; 126 else if (data instanceof Hashtable ) { 127 Hashtable parameters = (Hashtable ) data; 128 dtdToFind = (String ) parameters.get(DTD_TO_FIND); 129 elementToFind = (String ) parameters.get(ELEMENT_TO_FIND); 130 } 131 if (dtdToFind == null && elementToFind == null) { 132 String message = NLS.bind(ContentMessages.content_badInitializationData, XMLRootElementContentDescriber.class.getName()); 133 throw new CoreException(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, null)); 134 } 135 } 136 } 137 | Popular Tags |