KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > xml > EntityCatalogImpl


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.netbeans.core.xml;
21
22 import java.io.*;
23 import java.util.*;
24 import java.beans.*;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import org.xml.sax.*;
29 import org.xml.sax.helpers.*;
30 import org.openide.loaders.*;
31 import org.openide.cookies.InstanceCookie;
32 import org.openide.util.*;
33 import org.openide.xml.*;
34
35 /**
36  * This Entity Catalog implementation recognizes registrations defined at XMLayer.
37  *
38  * @author Petr Kuzel
39  * @version 1.0
40  */

41 public final class EntityCatalogImpl extends EntityCatalog {
42
43     /** map between publicId and privateId (String, String); must be synchronized */
44     private Map<String JavaDoc, String JavaDoc> id2uri; // accessed from SystemCatalogReader in xml/catalog by reflection
45

46     private static final RequestProcessor catalogRP = new RequestProcessor("EntityCatalog/parser"); // NOI18N
47

48     /** Creates new EntityCatalogImpl */
49     private EntityCatalogImpl(Map<String JavaDoc,String JavaDoc> map) {
50         id2uri = map;
51     }
52     
53     /**
54      * Resolve an entity using cached mapping.
55      */

56     public InputSource resolveEntity(String JavaDoc publicID, String JavaDoc systemID) {
57         if (publicID == null) return null;
58
59         String JavaDoc res = id2uri.get(publicID); // note this is synchronized Hashtable
60

61         InputSource ret = null;
62         if (res != null) {
63             ret = new InputSource(res);
64         }
65             
66 // System.err.println("" + publicID + " => " + ret);
67
return ret;
68     }
69
70     /**
71      * XMLDataObject.Processor implementation recognizing EntityCatalog.PUBLIC_ID DTDs
72      * giving them instance cookie returning registered entries.
73      */

74     public static class RegistrationProcessor extends DefaultHandler implements XMLDataObject.Processor, InstanceCookie, Runnable JavaDoc, PropertyChangeListener {
75
76         private XMLDataObject peer;
77         private Map<String JavaDoc, String JavaDoc> map;
78         private RequestProcessor.Task parsingTask = catalogRP.create(this);
79         private EntityCatalogImpl instance = null;
80
81         // Processor impl
82

83         public void attachTo (XMLDataObject xmlDO) {
84             
85             if (xmlDO == peer) return; //ignore double attachements
86

87             peer = xmlDO;
88             peer.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, peer)); //listen at PROP_DOCUMENT
89
parsingTask.schedule(0);
90         }
91
92         // DefaultHandler extension
93

94         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException {
95             if ("public".equals(qName)) { //NOI18N
96
String JavaDoc key = atts.getValue("publicId"); //NOI18N
97
String JavaDoc val = atts.getValue("uri"); //NOI18N
98

99                 if (key != null && val != null) {
100                     map.put(key, val);
101                 } else {
102                     throw new SAXException ("invalid <public> element: missing publicId or uri"); // NOI18N
103
}
104             }
105         }
106
107         public InputSource resolveEntity(String JavaDoc pid, String JavaDoc sid) {
108             if (EntityCatalog.PUBLIC_ID.equals(pid)) {
109                 // Don't use a nbres: URL here; can deadlock NbURLStreamHandlerFactory during startup
110
return new InputSource(EntityCatalogImpl.class.getClassLoader().getResource("org/openide/xml/EntityCatalog.dtd").toExternalForm()); // NOI18N
111
}
112             return null;
113         }
114
115         // Runnable impl (can be a task body)
116

117         public void run() {
118             map = new Hashtable<String JavaDoc, String JavaDoc>(); //be synchronized
119

120             try {
121                 String JavaDoc loc = peer.getPrimaryFile().getURL().toExternalForm();
122                 InputSource src = new InputSource(loc);
123
124                 // XXX(-ttran) don't validate
125
XMLReader reader = XMLUtil.createXMLReader(false);
126                 reader.setErrorHandler(this);
127                 reader.setContentHandler(this);
128                 reader.setEntityResolver(this);
129                 reader.parse(src);
130             } catch (SAXException ex) {
131                 // ignore
132
Logger.getLogger(EntityCatalogImpl.class.getName()).log(Level.WARNING, null, ex);
133             } catch (IOException ex) {
134                 // ignore
135
Logger.getLogger(EntityCatalogImpl.class.getName()).log(Level.WARNING, null, ex);
136             }
137         }
138
139         // InstanceCookie impl
140

141         public Class JavaDoc instanceClass() throws IOException, ClassNotFoundException JavaDoc {
142             return EntityCatalog.class;
143         }
144
145         /** We return singleton instance */
146         public Object JavaDoc instanceCreate() throws IOException, ClassNotFoundException JavaDoc {
147             
148             synchronized (this) {
149                 if (instance == null) {
150                     parsingTask.waitFinished();
151                     instance = new EntityCatalogImpl (map);
152                 }
153             }
154             return instance;
155         }
156
157         public String JavaDoc instanceName() {
158             return "org.openide.xml.EntityCatalog"; // NOI18N
159
}
160
161         /**
162           * Perform synchronous update on fileobject change.
163           */

164         public void propertyChange(PropertyChangeEvent e) {
165             
166             synchronized(this) {
167                 if (instance == null) return;
168             }
169             
170             if (XMLDataObject.PROP_DOCUMENT.equals(e.getPropertyName())) {
171                 // Please use ErrorManager if debugging messages are required
172
// (try TM.getErrorManager().getInstance(thisClassName).log(message))
173
//System.err.println("XML file have changed. reparsing " + peer.getPrimaryFile() ); // NOI18N
174
//update it sync
175
run();
176                 instance.id2uri = map; //replace map
177
}
178         }
179         
180     }
181 }
182
Popular Tags