1 3 19 20 package com.sun.org.apache.xml.internal.resolver.readers; 21 22 import java.util.Hashtable ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import java.net.MalformedURLException ; 28 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.DocumentBuilder ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 33 import com.sun.org.apache.xml.internal.resolver.Catalog; 34 import com.sun.org.apache.xml.internal.resolver.CatalogException; 35 import com.sun.org.apache.xml.internal.resolver.readers.CatalogReader; 36 import com.sun.org.apache.xml.internal.resolver.helpers.Namespaces; 37 38 import org.xml.sax.SAXException ; 39 import org.w3c.dom.*; 40 41 72 public class DOMCatalogReader implements CatalogReader { 73 80 protected Hashtable namespaceMap = new Hashtable (); 81 82 93 public void setCatalogParser(String namespaceURI, 94 String rootElement, 95 String parserClass) { 96 if (namespaceURI == null) { 97 namespaceMap.put(rootElement, parserClass); 98 } else { 99 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass); 100 } 101 } 102 103 113 public String getCatalogParser(String namespaceURI, 114 String rootElement) { 115 if (namespaceURI == null) { 116 return (String ) namespaceMap.get(rootElement); 117 } else { 118 return (String ) namespaceMap.get("{"+namespaceURI+"}"+rootElement); 119 } 120 } 121 122 125 public DOMCatalogReader() { } 126 127 153 public void readCatalog(Catalog catalog, InputStream is) 154 throws IOException , CatalogException { 155 156 DocumentBuilderFactory factory = null; 157 DocumentBuilder builder = null; 158 159 factory = DocumentBuilderFactory.newInstance(); 160 factory.setNamespaceAware(false); 161 factory.setValidating(false); 162 try { 163 builder = factory.newDocumentBuilder(); 164 } catch (ParserConfigurationException pce) { 165 throw new CatalogException(CatalogException.UNPARSEABLE); 166 } 167 168 Document doc = null; 169 170 try { 171 doc = builder.parse(is); 172 } catch (SAXException se) { 173 throw new CatalogException(CatalogException.UNKNOWN_FORMAT); 174 } 175 176 Element root = doc.getDocumentElement(); 177 178 String namespaceURI = Namespaces.getNamespaceURI(root); 179 String localName = Namespaces.getLocalName(root); 180 181 String domParserClass = getCatalogParser(namespaceURI, 182 localName); 183 184 if (domParserClass == null) { 185 if (namespaceURI == null) { 186 catalog.getCatalogManager().debug.message(1, "No Catalog parser for " 187 + localName); 188 } else { 189 catalog.getCatalogManager().debug.message(1, "No Catalog parser for " 190 + "{" + namespaceURI + "}" 191 + localName); 192 } 193 return; 194 } 195 196 DOMCatalogParser domParser = null; 197 198 try { 199 domParser = (DOMCatalogParser) Class.forName(domParserClass).newInstance(); 200 } catch (ClassNotFoundException cnfe) { 201 catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass); 202 throw new CatalogException(CatalogException.UNPARSEABLE); 203 } catch (InstantiationException ie) { 204 catalog.getCatalogManager().debug.message(1, "Cannot instantiate XML Catalog Parser class", domParserClass); 205 throw new CatalogException(CatalogException.UNPARSEABLE); 206 } catch (IllegalAccessException iae) { 207 catalog.getCatalogManager().debug.message(1, "Cannot access XML Catalog Parser class", domParserClass); 208 throw new CatalogException(CatalogException.UNPARSEABLE); 209 } catch (ClassCastException cce ) { 210 catalog.getCatalogManager().debug.message(1, "Cannot cast XML Catalog Parser class", domParserClass); 211 throw new CatalogException(CatalogException.UNPARSEABLE); 212 } 213 214 Node node = root.getFirstChild(); 215 while (node != null) { 216 domParser.parseCatalogEntry(catalog, node); 217 node = node.getNextSibling(); 218 } 219 } 220 221 237 public void readCatalog(Catalog catalog, String fileUrl) 238 throws MalformedURLException , IOException , CatalogException { 239 URL url = new URL (fileUrl); 240 URLConnection urlCon = url.openConnection(); 241 readCatalog(catalog, urlCon.getInputStream()); 242 } 243 } 244 | Popular Tags |