KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > descriptor > EntityResolver


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Sylvain Leblanc.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.descriptor;
28
29 // SAX imports
30
import org.xml.sax.InputSource JavaDoc;
31
32 // importing CCM DTDs beans
33
import org.objectweb.openccm.descriptor.componentassembly.beans.ComponentassemblyBeanImpl;
34 import org.objectweb.openccm.descriptor.corbacomponent.beans.CorbacomponentBeanImpl;
35 import org.objectweb.openccm.descriptor.softpkg.beans.SoftpkgBeanImpl;
36 import org.objectweb.openccm.descriptor.properties.beans.PropertiesBeanImpl;
37
38 public class EntityResolver implements org.xml.sax.EntityResolver JavaDoc {
39
40     // ==================================================================
41
//
42
// Internal state.
43
//
44
// ==================================================================
45

46     /**
47      * CORBA Software Descriptor Formal Public Identifier.
48      * This identifier is "-//OMG//DTD CORBA Software Descriptor 3.0//EN".
49      */

50     public static final String JavaDoc CSD_FPI_3_0 = "-//OMG//DTD CORBA Software Descriptor 3.0//EN";
51
52     /**
53      * CORBA Component Descriptor Formal Public Identifier.
54      * This identifier is "-//OMG//DTD CORBA Component Descriptor 3.0//EN".
55      */

56     public static final String JavaDoc CCD_FPI_3_0 = "-//OMG//DTD CORBA Component Descriptor 3.0//EN";
57
58     /**
59      * Component Assembly Descriptor Formal Public Identifier.
60      * This identifier is "-//OMG//DTD Component Assembly Descriptor 3.0//EN".
61      */

62     public static final String JavaDoc CAD_FPI_3_0 = "-//OMG//DTD Component Assembly Descriptor 3.0//EN";
63
64     /**
65      * Component Property File Formal Public Identifier.
66      * This identifier is "-//OMG//DTD Component Property File 3.0//EN".
67      */

68     public static final String JavaDoc CPF_FPI_3_0 = "-//OMG//DTD Component Property File 3.0//EN";
69     
70     /** The CCM entity resolver singleton. */
71     private static EntityResolver resolver_singleton=null;
72
73     // ==================================================================
74
//
75
// Constructors.
76
//
77
// ==================================================================
78

79     // ==================================================================
80
//
81
// Internal methods.
82
//
83
// ==================================================================
84

85     /**
86      * Returns a character stream reader on a file loaded in memory by
87      * the current context class loader.
88      *
89      * @param name The name of the file to read.
90      *
91      * @return The character stream reader on the given file.
92      */

93     protected java.io.Reader JavaDoc getReaderInMem(String JavaDoc name) {
94         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
95         return new java.io.InputStreamReader JavaDoc(loader.getResourceAsStream(name));
96     }
97
98     // ==================================================================
99
//
100
// Public methods.
101
//
102
// ==================================================================
103

104     /**
105      * Resolve CCM DTDs based on CCM FPI described by {@link
106      * #CAD_FPI_3_0}, {@link #CSD_FPI_3_0}, {@link #CCD_FPI_3_0},
107      * {@link #CPF_FPI_3_0}. The implementation try to resolve
108      * "properties.dtd", "corbacomponent.dtd", "softpkg.dtd",
109      * "componentassembly.dtd" in the jars loaded by the context class
110      * loader.
111      *
112      * @param publicId The public identifier. Supported identifiers
113      * are {@link #CAD_FPI_3_0}, {@link #CSD_FPI_3_0}, {@link
114      * #CCD_FPI_3_0} and {@link #CPF_FPI_3_0}.
115      * @param systemId An alternative system identifier for dtds.
116      *
117      * @return An input source on the pointed DTD.
118      */

119     public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId)
120     {
121         java.io.Reader JavaDoc reader = null;
122         if (publicId != null) {
123             if (publicId.equals(CPF_FPI_3_0)) {
124                 reader = getReaderInMem("properties.dtd");
125             } else if (publicId.equals(CCD_FPI_3_0)) {
126                 reader = getReaderInMem("corbacomponent.dtd");
127             } else if (publicId.equals(CSD_FPI_3_0)) {
128                 reader = getReaderInMem("softpkg.dtd");
129             } else if (publicId.equals(CAD_FPI_3_0)) {
130                 reader = getReaderInMem("componentassembly.dtd");
131             }
132         }
133         if (systemId != null && reader == null) {
134             return new InputSource JavaDoc(systemId);
135         }
136         if (reader == null) return null;
137         return new InputSource JavaDoc(reader);
138     }
139
140     /**
141      * Register an EntityResolver for all CCM DTDs root element bean.
142      */

143     public static void setCCMResolver() {
144         if (resolver_singleton == null) {
145             resolver_singleton = new EntityResolver();
146             ComponentassemblyBeanImpl.setEntityResolver(resolver_singleton);
147             CorbacomponentBeanImpl.setEntityResolver(resolver_singleton);
148             SoftpkgBeanImpl.setEntityResolver(resolver_singleton);
149             PropertiesBeanImpl.setEntityResolver(resolver_singleton);
150         }
151     }
152 }
153
Popular Tags