1 3 56 57 package org.jboss.util.xml.catalog.readers; 58 59 import java.util.Hashtable ; 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.net.URL ; 63 import java.net.URLConnection ; 64 import java.net.MalformedURLException ; 65 66 import javax.xml.parsers.DocumentBuilderFactory ; 67 import javax.xml.parsers.DocumentBuilder ; 68 import javax.xml.parsers.ParserConfigurationException ; 69 70 import org.jboss.util.xml.catalog.Catalog; 71 import org.jboss.util.xml.catalog.CatalogException; 72 import org.jboss.util.xml.catalog.helpers.Namespaces; 73 import org.jboss.util.xml.catalog.readers.CatalogReader; 74 75 import org.xml.sax.SAXException ; 76 import org.w3c.dom.*; 77 78 109 public class DOMCatalogReader implements CatalogReader { 110 117 protected Hashtable namespaceMap = new Hashtable (); 118 119 130 public void setCatalogParser(String namespaceURI, 131 String rootElement, 132 String parserClass) { 133 if (namespaceURI == null) { 134 namespaceMap.put(rootElement, parserClass); 135 } else { 136 namespaceMap.put("{"+namespaceURI+"}"+rootElement, parserClass); 137 } 138 } 139 140 150 public String getCatalogParser(String namespaceURI, 151 String rootElement) { 152 if (namespaceURI == null) { 153 return (String ) namespaceMap.get(rootElement); 154 } else { 155 return (String ) namespaceMap.get("{"+namespaceURI+"}"+rootElement); 156 } 157 } 158 159 162 public DOMCatalogReader() { } 163 164 190 public void readCatalog(Catalog catalog, InputStream is) 191 throws IOException , CatalogException { 192 193 DocumentBuilderFactory factory = null; 194 DocumentBuilder builder = null; 195 196 factory = DocumentBuilderFactory.newInstance(); 197 factory.setNamespaceAware(false); 198 factory.setValidating(false); 199 try { 200 builder = factory.newDocumentBuilder(); 201 } catch (ParserConfigurationException pce) { 202 throw new CatalogException(CatalogException.UNPARSEABLE); 203 } 204 205 Document doc = null; 206 207 try { 208 doc = builder.parse(is); 209 } catch (SAXException se) { 210 throw new CatalogException(CatalogException.UNKNOWN_FORMAT); 211 } 212 213 Element root = doc.getDocumentElement(); 214 215 String namespaceURI = Namespaces.getNamespaceURI(root); 216 String localName = Namespaces.getLocalName(root); 217 218 String domParserClass = getCatalogParser(namespaceURI, 219 localName); 220 221 if (domParserClass == null) { 222 if (namespaceURI == null) { 223 catalog.getCatalogManager().debug.message(1, "No Catalog parser for " 224 + localName); 225 } else { 226 catalog.getCatalogManager().debug.message(1, "No Catalog parser for " 227 + "{" + namespaceURI + "}" 228 + localName); 229 } 230 return; 231 } 232 233 DOMCatalogParser domParser = null; 234 235 try { 236 domParser = (DOMCatalogParser) Class.forName(domParserClass).newInstance(); 237 } catch (ClassNotFoundException cnfe) { 238 catalog.getCatalogManager().debug.message(1, "Cannot load XML Catalog Parser class", domParserClass); 239 throw new CatalogException(CatalogException.UNPARSEABLE); 240 } catch (InstantiationException ie) { 241 catalog.getCatalogManager().debug.message(1, "Cannot instantiate XML Catalog Parser class", domParserClass); 242 throw new CatalogException(CatalogException.UNPARSEABLE); 243 } catch (IllegalAccessException iae) { 244 catalog.getCatalogManager().debug.message(1, "Cannot access XML Catalog Parser class", domParserClass); 245 throw new CatalogException(CatalogException.UNPARSEABLE); 246 } catch (ClassCastException cce ) { 247 catalog.getCatalogManager().debug.message(1, "Cannot cast XML Catalog Parser class", domParserClass); 248 throw new CatalogException(CatalogException.UNPARSEABLE); 249 } 250 251 Node node = root.getFirstChild(); 252 while (node != null) { 253 domParser.parseCatalogEntry(catalog, node); 254 node = node.getNextSibling(); 255 } 256 } 257 258 274 public void readCatalog(Catalog catalog, String fileUrl) 275 throws MalformedURLException , IOException , CatalogException { 276 URL url = new URL (fileUrl); 277 URLConnection urlCon = url.openConnection(); 278 readCatalog(catalog, urlCon.getInputStream()); 279 } 280 } 281 | Popular Tags |