001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * Caucho Technology permits modification and use of this file in
005: * source and binary form ("the Software") subject to the Caucho
006: * Developer Source License 1.1 ("the License") which accompanies
007: * this file. The License is also available at
008: * http://www.caucho.com/download/cdsl1-1.xtp
009: *
010: * In addition to the terms of the License, the following conditions
011: * must be met:
012: *
013: * 1. Each copy or derived work of the Software must preserve the copyright
014: * notice and this notice unmodified.
015: *
016: * 2. Each copy of the Software in source or binary form must include
017: * an unmodified copy of the License in a plain ASCII text file named
018: * LICENSE.
019: *
020: * 3. Caucho reserves all rights to its names, trademarks and logos.
021: * In particular, the names "Resin" and "Caucho" are trademarks of
022: * Caucho and may not be used to endorse products derived from
023: * this software. "Resin" and "Caucho" may not appear in the names
024: * of products derived from this software.
025: *
026: * This Software is provided "AS IS," without a warranty of any kind.
027: * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
028: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
029: * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
030: *
031: * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
032: * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR
033: * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE
034: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
035: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
036: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
037: * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY
038: * OF SUCH DAMAGES.
039: *
040: * @author Sam
041: */
042:
043: package com.caucho.j2ee;
044:
045: import com.caucho.config.ConfigException;
046: import com.caucho.util.L10N;
047:
048: import org.w3c.dom.DocumentType;
049: import org.w3c.dom.Element;
050:
051: /**
052: * The J2EE version of a configuration file.
053: */
054: public enum J2EEVersion {
055: J2EE12 {
056: public boolean hasFeaturesOf(J2EEVersion version) {
057: return version == J2EE12;
058: }
059: },
060:
061: J2EE13 {
062: public boolean hasFeaturesOf(J2EEVersion version) {
063: return version == J2EE12 || version == J2EE13;
064: }
065: },
066:
067: J2EE14 {
068: public boolean hasFeaturesOf(J2EEVersion version) {
069: return version == J2EE12 || version == J2EE13
070: || version == J2EE14;
071: }
072: },
073:
074: JAVAEE5 {
075: public boolean hasFeaturesOf(J2EEVersion version) {
076: return version == J2EE12 || version == J2EE13
077: || version == J2EE14 || version == JAVAEE5;
078: }
079: },
080:
081: RESIN {
082: public boolean hasFeaturesOf(J2EEVersion version) {
083: return true;
084: }
085: };
086:
087: private static final L10N L = new L10N(J2EEVersion.class);
088:
089: public static final String J2EE_NAMESPACE = "http://java.sun.com/xml/ns/j2ee";
090: public static final String JAVAEE_NAMESPACE = "http://java.sun.com/xml/ns/javaee";
091: public static final String RESIN_NAMESPACE = "http://caucho.com/ns/resin";
092:
093: /**
094: * Return a J2EEVersion based on the namespace, the version attribute,
095: * and the doctype.
096: *
097: * @param top the top level element of a configuration file.
098: */
099: public static J2EEVersion getJ2EEVersion(Element top) {
100: String version = top.getAttribute("version");
101: String ns = top.getNamespaceURI();
102:
103: if (version.length() > 0) {
104: if (ns == null)
105: throw new ConfigException(
106: L
107: .l(
108: "namespace is required because version is specified, either xmlns='{0}', xmlns='{1}', or xmlns='{2}'",
109: RESIN_NAMESPACE,
110: JAVAEE_NAMESPACE,
111: J2EE_NAMESPACE));
112:
113: if (ns.equals(J2EE_NAMESPACE)) {
114: if (version.equals("1.4"))
115: return J2EE14;
116: else
117: throw new ConfigException(
118: L
119: .l(
120: "version must be '{0}' for namespace '{1}'",
121: "1.4", ns));
122: } else if (ns.equals(JAVAEE_NAMESPACE)) {
123: if (version.equals("5") || version.equals("5.0")
124: || version.equals("2.5")) // XXX For TCK emitted web.xmls
125: return JAVAEE5;
126: else
127: throw new ConfigException(
128: L
129: .l(
130: "version must be '{0}' for namespace '{1}'",
131: "5", ns));
132: } else if (ns.equals(RESIN_NAMESPACE)) {
133: return RESIN;
134: }
135: } else if (ns != null) {
136: if (ns.equals(RESIN_NAMESPACE))
137: return RESIN;
138:
139: throw new ConfigException(
140: L
141: .l(
142: "unknown namespace '{0}', namespace must be one of xmlns='{1}', xmlns='{2}', or xmlns='{3}'",
143: ns, RESIN_NAMESPACE,
144: JAVAEE_NAMESPACE, J2EE_NAMESPACE));
145: }
146:
147: DocumentType doctype = top.getOwnerDocument().getDoctype();
148:
149: if (doctype != null) {
150: String 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: }
|