View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.xml.resolver;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  
22  import org.xml.sax.EntityResolver;
23  import org.xml.sax.InputSource;
24  import org.xml.sax.SAXException;
25  
26  /**
27   * Resolves the resource either as a file URL or relative to the given class in
28   * the class path.
29   *
30   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
31   * @version $Revision:591 $
32   */
33  public class FileEntityResolver implements EntityResolver
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The delegate to pass to if this resolver is not responsible. May be
43     * <code>null</code>.
44     */
45    private final EntityResolver parentEntityResolver;
46  
47    // ****************************** Initializer *******************************
48  
49    // ****************************** Constructors ******************************
50  
51    /**
52     * Default constructor.
53     *
54     * @param parentEntityResolver the delegate to pass to if this resolver is not
55     *          responsible. May be <code>null</code>.
56     */
57    public FileEntityResolver(final EntityResolver parentEntityResolver)
58    {
59      this.parentEntityResolver = parentEntityResolver;
60    }
61  
62    // ****************************** Inner Classes *****************************
63  
64    // ********************************* Methods ********************************
65  
66    // --- init -----------------------------------------------------------------
67  
68    // --- get&set --------------------------------------------------------------
69  
70    // --- business -------------------------------------------------------------
71  
72    /**
73     * Resolves the resource either as a file URL or relative to the given class
74     * in the class path.
75     *
76     * @param publicId The public identifier of the external entity being
77     *          referenced, or null if none was supplied.
78     * @param systemId The system identifier of the external entity being
79     *          referenced.
80     * @return the input source for the public and system identifier.
81     * @throws org.xml.sax.SAXException Any SAX exception, possibly wrapping
82     *           another exception.
83     * @throws java.io.IOException A Java-specific IO exception, possibly the
84     *           result of creating a new InputStream or Reader for the
85     *           InputSource.
86     */
87    @Override
88    public InputSource resolveEntity(final String publicId, final String systemId)
89      throws IOException, SAXException
90    {
91      if (systemId != null && systemId.startsWith("file:/"))
92      {
93        final File file = new File(systemId.substring(6));
94        if (file.canRead())
95        {
96          final InputSource source = new InputSource(new FileInputStream(file));
97          source.setPublicId(publicId);
98          source.setSystemId(systemId);
99          return source;
100       }
101       else
102       {
103         if (parentEntityResolver != null)
104         {
105           final String id = file.getName();
106           final InputSource source =
107               parentEntityResolver.resolveEntity(publicId, id);
108           source.setPublicId(publicId);
109           source.setSystemId(systemId);
110           return source;
111         }
112       }
113     }
114 
115     if (parentEntityResolver != null)
116     {
117       return parentEntityResolver.resolveEntity(publicId, systemId);
118     }
119     else
120     {
121       throw new IOException("Cannot resolve " + publicId + " / " + systemId
122                             + '.');
123     }
124   }
125 
126   // --- object basics --------------------------------------------------------
127 
128 }