1 | /* |
2 | * jDTAUS Core Utilities |
3 | * Copyright (C) 2005 Christian Schulte <cs@schulte.it> |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2.1 of the License, or any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18 | * |
19 | * $JDTAUS: EntityResolverChain.java 8855 2014-01-10 14:27:59Z schulte $ |
20 | */ |
21 | package org.jdtaus.core.sax.util; |
22 | |
23 | import java.io.IOException; |
24 | import java.util.Locale; |
25 | import org.jdtaus.core.container.ContainerFactory; |
26 | import org.jdtaus.core.logging.spi.Logger; |
27 | import org.xml.sax.EntityResolver; |
28 | import org.xml.sax.InputSource; |
29 | import org.xml.sax.SAXException; |
30 | |
31 | /** |
32 | * {@code EntityResolver} implementation backed by a chain of resolvers used |
33 | * for resolving entities. |
34 | * |
35 | * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> |
36 | * @version $JDTAUS: EntityResolverChain.java 8855 2014-01-10 14:27:59Z schulte $ |
37 | */ |
38 | public final class EntityResolverChain implements EntityResolver |
39 | { |
40 | //--EntityResolver---------------------------------------------------------- |
41 | |
42 | /** |
43 | * Resolves entities by querying all resolvers of the instance stopping at |
44 | * the first one not returning {@code null}. |
45 | * |
46 | * @param publicId The public identifier of the external entity being |
47 | * referenced, or {@code null} if none was supplied. |
48 | * @param systemId The system identifier of the external entity being |
49 | * referenced. |
50 | * |
51 | * @return An {@code InputSource} object describing the new input source, or |
52 | * {@code null} to request that the parser open a regular URI connection to |
53 | * the system identifier. |
54 | * |
55 | * @throws SAXException Any SAX exception, possibly wrapping another |
56 | * exception. |
57 | * @throws IOException A Java-specific IO exception, possibly the result of |
58 | * creating a new {@code InputStream} or {@code Reader} for the |
59 | * {@code InputSource}. |
60 | * |
61 | * @see EntityResolver#resolveEntity(java.lang.String, java.lang.String) |
62 | */ |
63 | public InputSource resolveEntity( final String publicId, |
64 | final String systemId ) |
65 | throws SAXException, IOException |
66 | { |
67 | InputSource inputSource = null; |
68 | for ( int i = this.getResolvers().length - 1; |
69 | i >= 0 && inputSource == null; i-- ) |
70 | { |
71 | final EntityResolver resolver = this.getResolvers()[i]; |
72 | inputSource = resolver.resolveEntity( publicId, systemId ); |
73 | } |
74 | |
75 | return inputSource; |
76 | } |
77 | |
78 | //----------------------------------------------------------EntityResolver-- |
79 | //--EntityResolverChain----------------------------------------------------- |
80 | |
81 | /** Chain of resolvers to use. */ |
82 | private EntityResolver[] resolvers; |
83 | |
84 | /** |
85 | * Creates a new {@code EntityResolverChain} instance backed by any |
86 | * available {@code EntityResolver} implementation in the system. |
87 | */ |
88 | public EntityResolverChain() |
89 | { |
90 | this( null ); |
91 | } |
92 | |
93 | /** |
94 | * Creates a new {@code EntityResolverChain} instance taking an array of |
95 | * resolvers to use for resolving entities. |
96 | * |
97 | * @param resolvers The resolvers to use for resolving entities. |
98 | */ |
99 | public EntityResolverChain( final EntityResolver[] resolvers ) |
100 | { |
101 | super(); |
102 | |
103 | if ( resolvers != null && resolvers.length > 0 ) |
104 | { |
105 | this.resolvers = (EntityResolver[]) resolvers.clone(); |
106 | } |
107 | } |
108 | |
109 | /** |
110 | * Gets the resolvers of the instance. |
111 | * |
112 | * @return the resolvers used for resolving entities. |
113 | */ |
114 | public EntityResolver[] getResolvers() |
115 | { |
116 | if ( this.resolvers == null ) |
117 | { |
118 | this.resolvers = this.getDefaultResolvers(); |
119 | |
120 | if ( this.resolvers.length == 0 ) |
121 | { |
122 | this.getLogger().warn( |
123 | this.getNoEntityResolversMessage( this.getLocale() ) ); |
124 | |
125 | } |
126 | } |
127 | |
128 | return (EntityResolver[]) this.resolvers.clone(); |
129 | } |
130 | |
131 | //-----------------------------------------------------EntityResolverChain-- |
132 | //--Dependencies------------------------------------------------------------ |
133 | |
134 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies |
135 | // This section is managed by jdtaus-container-mojo. |
136 | |
137 | /** |
138 | * Gets the configured <code>Logger</code> implementation. |
139 | * |
140 | * @return The configured <code>Logger</code> implementation. |
141 | */ |
142 | private Logger getLogger() |
143 | { |
144 | return (Logger) ContainerFactory.getContainer(). |
145 | getDependency( this, "Logger" ); |
146 | |
147 | } |
148 | |
149 | /** |
150 | * Gets the configured <code>Locale</code> implementation. |
151 | * |
152 | * @return The configured <code>Locale</code> implementation. |
153 | */ |
154 | private Locale getLocale() |
155 | { |
156 | return (Locale) ContainerFactory.getContainer(). |
157 | getDependency( this, "Locale" ); |
158 | |
159 | } |
160 | |
161 | /** |
162 | * Gets the configured <code>DefaultResolvers</code> implementation. |
163 | * |
164 | * @return The configured <code>DefaultResolvers</code> implementation. |
165 | */ |
166 | private EntityResolver[] getDefaultResolvers() |
167 | { |
168 | return (EntityResolver[]) ContainerFactory.getContainer(). |
169 | getDependency( this, "DefaultResolvers" ); |
170 | |
171 | } |
172 | |
173 | // </editor-fold>//GEN-END:jdtausDependencies |
174 | |
175 | //------------------------------------------------------------Dependencies-- |
176 | //--Messages---------------------------------------------------------------- |
177 | |
178 | // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausMessages |
179 | // This section is managed by jdtaus-container-mojo. |
180 | |
181 | /** |
182 | * Gets the text of message <code>noEntityResolvers</code>. |
183 | * <blockquote><pre>Keine ''EntityResolver'' gefunden. Java XML-Parser öffnen standardmäßig reguläre - eventuell entfernte - URI-Verbindungen. Sie sollten mindestens eine ''EntityResolver''-Implementierung (z.B. jdtaus-core-entity-resolver-1.15.jar) zur Verfügung stellen.</pre></blockquote> |
184 | * <blockquote><pre>No entity resolvers found. Standard Java XML parsers fall back to opening regular - possibly remote - URI connections. You should provide at least one ''EntityResolver'' implementation (e.g. jdtaus-core-entity-resolver-1.15.jar).</pre></blockquote> |
185 | * |
186 | * @param locale The locale of the message instance to return. |
187 | * |
188 | * @return the text of message <code>noEntityResolvers</code>. |
189 | */ |
190 | private String getNoEntityResolversMessage( final Locale locale ) |
191 | { |
192 | return ContainerFactory.getContainer(). |
193 | getMessage( this, "noEntityResolvers", locale, null ); |
194 | |
195 | } |
196 | |
197 | // </editor-fold>//GEN-END:jdtausMessages |
198 | |
199 | //----------------------------------------------------------------Messages-- |
200 | } |