KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > validation > impl > ValidationResolver


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
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 org.apache.cocoon.components.validation.impl;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.excalibur.source.Source;
24 import org.apache.excalibur.source.SourceResolver;
25 import org.apache.excalibur.source.SourceValidity;
26 import org.apache.excalibur.source.impl.validity.AggregatedValidity;
27 import org.xml.sax.EntityResolver JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30
31 /**
32  * <p>An internal {@link InputSource} resolver that can be used while parsing
33  * schemas.</p>
34  *
35  * <p>This instance will track all resolved external sources and will store their
36  * validity information. An aggregated {@link SourceValidity} for all resolved
37  * sources can be retrieved when {@link #close() closing} this instance.</p>
38  *
39  * @author <a HREF="mailto:pier@betaversion.org">Pier Fumagalli</a>
40  */

41 public class ValidationResolver implements EntityResolver JavaDoc {
42
43     /** <p>The {@link SourceResolver} to access {@link Source}s.</p> */
44     private final SourceResolver sourceResolver;
45     /** <p>The {@link EntityResolver} to resolve public IDs against catalogs.</p> */
46     private final EntityResolver JavaDoc entityResolver;
47     /** <p>The global {@link SourceValidity} of all resolved sources.</p> */
48     private final AggregatedValidity sourceValidity;
49
50     /** <p>A {@link List} of {@link Source} to be released when closing.</p> */
51     private final List JavaDoc sources = new ArrayList JavaDoc();
52
53     /** <p>A flag indicating whether this instance has been closed.</p> */
54     private boolean closed = false;
55
56     /**
57      * <p>Create a new {@link ValidationResolver} instance.</p>
58      *
59      * @throws NullPointerException if one of the specified {@link SourceResolver}
60      * or {@link EntityResolver} was <b>null</b>.
61      */

62     public ValidationResolver(SourceResolver sourceResolver,
63                               EntityResolver JavaDoc entityResolver) {
64         if (sourceResolver == null) throw new NullPointerException JavaDoc("Null source");
65         if (entityResolver == null) throw new NullPointerException JavaDoc("Null entity");
66         this.sourceValidity = new AggregatedValidity();
67         this.sourceResolver = sourceResolver;
68         this.entityResolver = entityResolver;
69     }
70
71     /**
72      * <p>Resolve a {@link Source} into an {@link InputSource}.</p>
73      */

74     public InputSource JavaDoc resolveSource(Source source)
75     throws IOException JavaDoc, SAXException JavaDoc {
76         return this.resolveSource(source, null, null);
77     }
78
79     /**
80      * <p>Resolve a {@link Source} into an {@link InputSource}, specifying a
81      * specific system identifier.</p>
82      */

83     public InputSource JavaDoc resolveSource(Source source, String JavaDoc systemId)
84     throws IOException JavaDoc, SAXException JavaDoc {
85         return this.resolveSource(source, systemId, null);
86     }
87
88     /**
89      * <p>Resolve a {@link Source} into an {@link InputSource}, specifying both
90      * a specific system identifier and a public identifier.</p>
91      *
92      * <p>If the specified system identifier was <b>null</b> the returned
93      * {@link InputSource}'s {@link InputSource#getSystemId() system identifier}
94      * will be obtained calling the {@link Source#getURI()} method.</p>
95      */

96     public InputSource JavaDoc resolveSource(Source source, String JavaDoc systemId, String JavaDoc publicId)
97     throws IOException JavaDoc, SAXException JavaDoc {
98         if (this.closed) throw new IllegalStateException JavaDoc("Resolver closed");
99
100         /* Validate what we've been passed */
101         if (source == null) throw new NullPointerException JavaDoc("Null source specified");
102
103         /* Record the current source in the validities to return */
104         this.sourceValidity.add(source.getValidity());
105
106         /* Ensure that we have a proper system id */
107         if (systemId == null) systemId = source.getURI();
108
109         /* Create a new input source and return it filled out entirely */
110         InputSource JavaDoc input = new InputSource JavaDoc(systemId);
111         input.setByteStream(source.getInputStream());
112         if (publicId != null) input.setPublicId(publicId);
113         return input;
114     }
115
116     /**
117      * <p>Resolve an entity identified by a specific system identifier as an
118      * {@link InputSource}.</p>
119      */

120     public InputSource JavaDoc resolveEntity(String JavaDoc systemId)
121     throws IOException JavaDoc, SAXException JavaDoc {
122         return this.resolveEntity(null, null, systemId);
123     }
124
125     /**
126      * <p>Resolve an entity identified by a specific system and public identifier
127      * as an {@link InputSource}.</p>
128      */

129     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
130     throws IOException JavaDoc, SAXException JavaDoc {
131         return this.resolveEntity(null, publicId, systemId);
132     }
133
134     /**
135      * <p>Resolve an entity identified by a specific system and public identifier
136      * and relative to a specified base location as an {@link InputSource}.</p>
137      */

138     public InputSource JavaDoc resolveEntity(String JavaDoc base, String JavaDoc publicId, String JavaDoc systemId)
139     throws IOException JavaDoc, SAXException JavaDoc {
140         if (this.closed) throw new IllegalStateException JavaDoc("Resolver closed");
141
142         /* If the specified system id was null use the global entity resolver */
143         if (systemId == null) {
144             InputSource JavaDoc source = this.entityResolver.resolveEntity(publicId, null);
145             if ((source == null) || (source.getSystemId() == null)) {
146                 throw new IOException JavaDoc("Can't resolve \"" + publicId + "\"");
147             }
148             systemId = source.getSystemId();
149         }
150
151         /* Now that we have a valid system id, attempt to resolve it as a source */
152         final Source source;
153         if (base == null) {
154             source = this.sourceResolver.resolveURI(systemId);
155         } else {
156             source = this.sourceResolver.resolveURI(systemId, base, null);
157         }
158
159         /* Record this source as a source to release back to the resolver */
160         this.sources.add(source);
161
162         /* Return the resolved input source back to the caller */
163         return this.resolveSource(source, systemId, publicId);
164     }
165
166     /**
167      * <p>Close this {@link ValidationResolver} instance, releasing all created
168      * {@link Source}s back to the {@link SourceResolver} and returning an
169      * aggregated {@link SourceValidity}.</p>
170      */

171     public SourceValidity close() {
172
173         /* Release all the sources that were opened using this source resolver */
174         Iterator JavaDoc iterator = this.sources.iterator();
175         while (iterator.hasNext()) {
176             this.sourceResolver.release((Source) iterator.next());
177         }
178
179         /* Mark this instance as closed */
180         this.closed = true;
181
182         /* Return the source validity associated with this instance */
183         return this.sourceValidity;
184     }
185
186     /**
187      * <p>Ensure that when this object is garbage collected, the {@link #close()}
188      * method is executed.</p>
189      */

190     protected void finalize()
191     throws Throwable JavaDoc {
192         try {
193             super.finalize();
194         } finally {
195             if (this.closed) return;
196             this.close();
197         }
198     }
199 }
200
Popular Tags