KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > catalog > CatalogEntityResolver


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 package org.netbeans.modules.xml.catalog;
20
21 import java.util.*;
22 import java.io.*;
23 import java.net.URL JavaDoc;
24
25 import org.xml.sax.*;
26 import org.netbeans.modules.xml.catalog.spi.*;
27 import org.netbeans.modules.xml.catalog.lib.*;
28 import org.netbeans.modules.xml.catalog.settings.CatalogSettings;
29
30 import org.netbeans.api.xml.services.*;
31 import org.openide.util.Lookup;
32 import javax.xml.transform.URIResolver JavaDoc;
33
34 /**
35  * An entity resolver that can resolve all registrations
36  * in catalogs mounted by a user.
37  * This is not exposed catalog package API. The
38  * package funtionality is exposed via registering
39  * this entity resolver in XMLDataObject resolver chain.
40  * <p>
41  * The class is public only for internal XML module reasons.
42  *
43  * @author Petr Kuzel
44  * @version 1.0
45  */

46 public class CatalogEntityResolver extends UserCatalog implements EntityResolver, URIResolver JavaDoc {
47
48     /** Creates new CatalogEntityResolver */
49     public CatalogEntityResolver() {
50     }
51
52     public EntityResolver getEntityResolver() {
53         return this;
54     }
55     
56     /**
57      * User's JAXP/TrAX <code>URIResolver</code>.
58      * @return URIResolver or <code>null</code> if not supported.
59      */

60     public URIResolver JavaDoc getURIResolver() {
61         return this;
62     }
63     
64     // SAX interface method implementation
65
public InputSource resolveEntity(String JavaDoc publicId,String JavaDoc systemId)
66         throws SAXException, IOException {
67         InputSource result = null;
68         Iterator it = null;
69         
70         // try to use full featured entiry resolvers
71

72         CatalogSettings mounted = CatalogSettings.getDefault();
73         it = mounted.getCatalogs( new Class JavaDoc[] {EntityResolver.class});
74
75         while (it.hasNext()) {
76             EntityResolver next = (EntityResolver) it.next();
77             result = next.resolveEntity(publicId, systemId);
78             if (result != null) break;
79         }
80         
81         // fallback to ordinaly readers
82

83         if (result == null && publicId != null) {
84             
85             it = mounted.getCatalogs(new Class JavaDoc[] {CatalogReader.class});
86
87             while (it.hasNext()) {
88                 CatalogReader next = (CatalogReader) it.next();
89                 String JavaDoc sid = next.getSystemID(publicId);
90                 if (sid != null) {
91                     result = new InputSource(sid);
92                     break;
93                 }
94             }
95         }
96         
97         // return result (null is allowed)
98

99         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("CatalogEntityResolver:PublicID: " + publicId + ", " + systemId + " => " + (result == null ? "null" : result.getSystemId())); // NOI18N
100

101         // #56103 bootstrap XML catalog DTD
102
if (result == null && "-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN".equals(publicId)) { // NOi18N
103
URL JavaDoc url = org.apache.xml.resolver.Catalog.class.getResource("etc/catalog.dtd"); // NOI18N
104
result = new InputSource(url.toExternalForm());
105         }
106
107         //#53710 URL space canonization (%20 form works in most cases)
108
if (result != null) {
109             String JavaDoc patchedSystemId = result.getSystemId();
110             if (patchedSystemId != null) {
111                 patchedSystemId = patchedSystemId.replaceAll("\\+", "%20"); // NOI18N
112
patchedSystemId = patchedSystemId.replaceAll("\\ ", "%20"); // NOI18N
113
result.setSystemId(patchedSystemId);
114             }
115         }
116         return result;
117         
118     }
119     
120     /**
121      * Return all known public IDs.
122      */

123     public Iterator getPublicIDs() {
124         
125         IteratorIterator ret = new IteratorIterator();
126         
127         CatalogSettings mounted = CatalogSettings.getDefault();
128         Iterator it = mounted.getCatalogs( new Class JavaDoc[] {CatalogReader.class});
129
130         while (it.hasNext()) {
131             CatalogReader next = (CatalogReader) it.next();
132             Iterator ids = next.getPublicIDs();
133             if (ids != null) {
134                 ret.add(ids);
135             }
136         }
137         
138         return ret;
139     }
140
141     public javax.xml.transform.Source JavaDoc resolve(String JavaDoc publicId, String JavaDoc systemId)
142         throws javax.xml.transform.TransformerException JavaDoc {
143             
144         //throws SAXException, IOException {
145

146         javax.xml.transform.Source JavaDoc result = null;
147         
148         // try to use full featured entiry resolvers
149

150         CatalogSettings mounted = CatalogSettings.getDefault();
151
152         if (publicId != null) {
153             
154             Iterator it = mounted.getCatalogs(new Class JavaDoc[] {CatalogReader.class});
155
156             while (it.hasNext()) {
157                 CatalogReader next = (CatalogReader) it.next();
158                 try {
159                     String JavaDoc sid=null;
160                     if (publicId.startsWith("urn:publicid:")) { //NOI18N
161
// resolving publicId from catalog
162
String JavaDoc urn = publicId.substring(13);
163                         sid=next.resolvePublic(URNtoPublic(urn));
164                     } else sid = next.resolveURI(publicId);
165                     if (sid != null) {
166                         javax.xml.transform.Source JavaDoc source = new javax.xml.transform.sax.SAXSource JavaDoc();
167                         source.setSystemId(sid);
168                         result=source;
169                         break;
170                     }
171                 } catch (java.lang.Error JavaDoc error) {}
172             }
173         }
174         
175         // return result (null is allowed)
176

177         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("CatalogEntityResolver:PublicID: " + publicId + ", " + systemId + " => " + (result == null ? "null" : result.getSystemId())); // NOI18N
178
return result;
179     }
180     
181     /** Conversion of URN string to public identifier
182      * see : http://www.faqs.org/rfcs/rfc3151.html
183      */

184     private String JavaDoc URNtoPublic(String JavaDoc urn) {
185         return urn.replace('+',' ').replaceAll(":","//").replaceAll(";","::").replaceAll("%2B","+").replaceAll("%3A",":").replaceAll("%2F","/").replaceAll("%3B",";").replaceAll("%27","'").replaceAll("%3F","?").replaceAll("%23","#").replaceAll("%25","%");
186     }
187 }
188
Popular Tags