KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > XMLEntityResolverChain


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.loaders;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.xml.sax.*;
26
27 /**
28  * A utility class allowing to chain multiple EntityResolvers into one delegating
29  * resolution process on them.
30  * <p>
31  * If all registered resolves are thread-safe then the implementation is thread-safe too.
32  *
33  * @author Petr Kuzel
34  * @version 1.0
35  */

36 final class XMLEntityResolverChain implements EntityResolver {
37
38     /**
39      * Chain of resolvers contaning all EntityResolvers registred by a user.
40      */

41     private final List<EntityResolver> resolverChain = new ArrayList<EntityResolver>(3);
42     
43     
44     /** Creates new EntityResolverChain instance */
45     public XMLEntityResolverChain() {
46     }
47     
48     /**
49      * Add a given entity resolver to the resolver chain.
50      * The resolver chain is then searched by resolveEntity() method
51      * until some registered resolver succed.
52      *
53      * @param resolver - a non null resolver to be added
54      *
55      * @return true if successfully added
56      */

57     public boolean addEntityResolver(EntityResolver resolver) {
58         if (resolver == null) throw new IllegalArgumentException JavaDoc();
59         synchronized (resolverChain) {
60             if (resolverChain.contains(resolver)) return false;
61             return resolverChain.add(resolver);
62         }
63     }
64
65     /**
66      * Remove a given entity resolver from the entity resolver chain.
67      *
68      * @param resolver - a non null resolver to be removed
69      *
70      * @return removed resolver instance or null if not present
71      */

72     public EntityResolver removeEntityResolver(EntityResolver resolver) {
73         if (resolver == null) throw new IllegalArgumentException JavaDoc();
74         synchronized (resolverChain) {
75             int index = resolverChain.indexOf(resolver);
76             if ( index < 0 ) return null;
77             return (EntityResolver) resolverChain.remove(index);
78         }
79     }
80
81     /**
82      * Chaining resolveEntity() implementation iterating over registered resolvers.
83      *
84      * @param publicID - The public identifier of the external entity being referenced,
85      * or null if none was supplied.
86      * @param systemID - The system identifier of the external entity being referenced.
87      *
88      * @throw SAXException - Any SAX exception, possibly wrapping another exception.
89      * @throw IOException - A Java-specific IO exception, possibly the result of creating
90      * a new InputStream or Reader for the InputSource.
91      *
92      * @return An InputSource object describing the new input source,
93      * or null to request that the parser open a regular URI connection to the system identifier.
94      */

95     public InputSource resolveEntity(String JavaDoc publicID, String JavaDoc systemID) throws SAXException, IOException {
96         
97         // user's resolver chain
98
SAXException lsex = null;
99         IOException lioex = null;
100         
101         synchronized (resolverChain) {
102             Iterator it = resolverChain.iterator();
103             while (it.hasNext()) {
104                 EntityResolver resolver = (EntityResolver) it.next();
105                 try {
106                     InputSource test = resolver.resolveEntity(publicID, systemID);
107                     if (test == null) continue;
108                     return test;
109                 } catch (SAXException sex) {
110                     lsex = sex;
111                     continue;
112                 } catch (IOException ioex) {
113                     lioex = ioex;
114                     continue;
115                 }
116             }
117             
118             //retain the last exception for diagnostics purposes
119

120             if (lioex != null) throw lioex;
121             if (lsex != null) throw lsex;
122             
123             //just can not resolve it
124
return null;
125         }
126     }
127 }
128
Popular Tags