KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > j2ee > J2EEVersion


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * Caucho Technology permits modification and use of this file in
5  * source and binary form ("the Software") subject to the Caucho
6  * Developer Source License 1.1 ("the License") which accompanies
7  * this file. The License is also available at
8  * http://www.caucho.com/download/cdsl1-1.xtp
9  *
10  * In addition to the terms of the License, the following conditions
11  * must be met:
12  *
13  * 1. Each copy or derived work of the Software must preserve the copyright
14  * notice and this notice unmodified.
15  *
16  * 2. Each copy of the Software in source or binary form must include
17  * an unmodified copy of the License in a plain ASCII text file named
18  * LICENSE.
19  *
20  * 3. Caucho reserves all rights to its names, trademarks and logos.
21  * In particular, the names "Resin" and "Caucho" are trademarks of
22  * Caucho and may not be used to endorse products derived from
23  * this software. "Resin" and "Caucho" may not appear in the names
24  * of products derived from this software.
25  *
26  * This Software is provided "AS IS," without a warranty of any kind.
27  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
28  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
29  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
30  *
31  * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
32  * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
33  * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
34  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
35  * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
36  * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
37  * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
38  * OF SUCH DAMAGES.
39  *
40  * @author Sam
41  */

42
43 package com.caucho.j2ee;
44
45 import com.caucho.config.ConfigException;
46 import com.caucho.util.L10N;
47
48 import org.w3c.dom.DocumentType JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50
51 /**
52  * The J2EE version of a configuration file.
53  */

54 public enum J2EEVersion {
55   J2EE12 {
56     public boolean hasFeaturesOf(J2EEVersion version)
57     {
58       return version == J2EE12;
59     }
60   },
61
62   J2EE13 {
63     public boolean hasFeaturesOf(J2EEVersion version)
64     {
65       return version == J2EE12
66              || version == J2EE13;
67     }
68   },
69
70   J2EE14 {
71     public boolean hasFeaturesOf(J2EEVersion version)
72     {
73       return version == J2EE12
74              || version == J2EE13
75              || version == J2EE14;
76     }
77   },
78
79   JAVAEE5 {
80     public boolean hasFeaturesOf(J2EEVersion version)
81     {
82       return version == J2EE12
83              || version == J2EE13
84              || version == J2EE14
85              || version == JAVAEE5;
86     }
87   },
88
89   RESIN {
90     public boolean hasFeaturesOf(J2EEVersion version)
91     {
92       return true;
93     }
94   };
95
96   private static final L10N L = new L10N(J2EEVersion.class);
97
98   public static final String JavaDoc J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
99   public static final String JavaDoc JAVAEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
100   public static final String JavaDoc RESIN_NAMESPACE = "http://caucho.com/ns/resin";
101
102   /**
103    * Return a J2EEVersion based on the namespace, the version attribute,
104    * and the doctype.
105    *
106    * @param top the top level element of a configuration file.
107    */

108   public static J2EEVersion getJ2EEVersion(Element JavaDoc top)
109   {
110     String JavaDoc version = top.getAttribute("version");
111     String JavaDoc ns = top.getNamespaceURI();
112
113     if (version.length() > 0) {
114       if (ns == null)
115         throw new ConfigException(L.l("namespace is required because version is specified, either xmlns='{0}', xmlns='{1}', or xmlns='{2}'",
116                                       RESIN_NAMESPACE,
117                                       JAVAEE_NAMESPACE,
118                                       J2EE_NAMESPACE));
119
120       if (ns.equals(J2EE_NAMESPACE)) {
121         if (version.equals("1.4"))
122           return J2EE14;
123         else
124           throw new ConfigException(L.l("version must be '{0}' for namespace '{1}'", "1.4", ns));
125       }
126       else if (ns.equals(JAVAEE_NAMESPACE)) {
127         if (version.equals("5"))
128           return JAVAEE5;
129         else
130           throw new ConfigException(L.l("version must be '{0}' for namespace '{1}'", "5", ns));
131       }
132       else if (ns.equals(RESIN_NAMESPACE)) {
133         return RESIN;
134       }
135     }
136     else if (ns != null) {
137       if (ns.equals(RESIN_NAMESPACE))
138         return RESIN;
139
140       throw new ConfigException(L.l("unknown namespace '{0}', namespace must be one of xmlns='{1}', xmlns='{2}', or xmlns='{3}'",
141                                     ns,
142                                     RESIN_NAMESPACE,
143                                     JAVAEE_NAMESPACE,
144                                     J2EE_NAMESPACE));
145     }
146
147     DocumentType JavaDoc doctype = top.getOwnerDocument().getDoctype();
148
149     if (doctype != null) {
150       String JavaDoc publicId = doctype.getPublicId();
151
152       if (publicId != null) {
153         if (publicId.contains("1.3"))
154           return J2EE13;
155       }
156     }
157
158     return J2EE12;
159   }
160
161   /**
162    * Returns true if this version is equal to or more recent than the
163    * passed version.
164    */

165   abstract public boolean hasFeaturesOf(J2EEVersion version);
166 }
167
Popular Tags