minor
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / jaxb / CdmResourceResolver.java
1 /**
2 * Copyright (C) 2008 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.io.jaxb;
11
12 import java.io.IOException;
13 import java.util.Properties;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.xml.resolver.CatalogManager;
18 import org.apache.xml.resolver.tools.CatalogResolver;
19 import org.w3c.dom.ls.LSInput;
20 import org.w3c.dom.ls.LSResourceResolver;
21 import org.xml.sax.InputSource;
22
23 /**
24 * Copied from CATE (@author ben.clark). Not used at this point.
25 */
26 public class CdmResourceResolver implements LSResourceResolver {
27 private final CatalogResolver catalogResolver;
28 private static Log log = LogFactory.getLog(CdmResourceResolver.class);
29
30 public CdmResourceResolver() throws IOException {
31 Properties properties = new Properties();
32 if(log.isInfoEnabled()) {
33 log.info("Loading /CatalogManager.properties");
34 }
35 properties.load(this.getClass().getResourceAsStream("/CatalogManager.properties"));
36
37 String[] catalogFileNames = properties.getProperty("catalogs").split(",");
38
39 CatalogManager catalogManager = new CatalogManager();
40 catalogManager.setRelativeCatalogs(true);
41 catalogResolver = new CatalogResolver(catalogManager);
42
43 for(String catalogFileName : catalogFileNames) {
44 if(log.isInfoEnabled()) {
45 log.info("Parsing " + catalogFileName);
46 }
47 catalogResolver.getCatalog().parseCatalog(this.getClass().getResource(catalogFileName));
48 }
49 }
50
51 public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
52
53 if(log.isInfoEnabled()) {
54 log.info("Resolving " + namespaceURI + " with systemId " + systemId + ", trying " + systemId);
55 }
56 InputSource inputSource = catalogResolver.resolveEntity( publicId, systemId );
57 if ( inputSource == null ) {
58 if(log.isInfoEnabled()) {
59 log.info("Not found in filesystem: Looking in jar files for /schema/cdm/" + systemId);
60 }
61 inputSource = new InputSource(this.getClass().getResourceAsStream("/schema/cdm/" + systemId));
62
63 if(inputSource == null) {
64 if(log.isWarnEnabled()) {
65 log.warn(namespaceURI + " not found");
66 }
67 return null;
68 }
69 }
70
71 if(log.isInfoEnabled()) {
72 // log.info("Resource found");
73 }
74 LsInputImpl lsInput = new LsInputImpl();
75 lsInput.setByteStream( inputSource.getByteStream() );
76 lsInput.setCharacterStream( inputSource.getCharacterStream() );
77 lsInput.setPublicId( inputSource.getPublicId() );
78 lsInput.setSystemId( inputSource.getSystemId() );
79 lsInput.setEncoding( inputSource.getEncoding() );
80 return lsInput;
81 }
82
83 }