KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > serializers > util > DocType


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.serializers.util;
17
18 import org.apache.commons.lang.builder.EqualsBuilder;
19 import org.apache.commons.lang.builder.HashCodeBuilder;
20
21 /**
22  * The <code>DocType</code> class encapsulates informations regarding
23  * the document type public and system IDs and root element name.
24  *
25  * @author <a HREF="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
26  * @version CVS $Id: DocType.java 123897 2005-01-02 18:41:35Z antonio $
27  */

28 public class DocType {
29
30     private static final char S_DOCTYPE_1[] = "<!DOCTYPE ".toCharArray();
31     private static final char S_DOCTYPE_2[] = " PUBLIC \"".toCharArray();
32     private static final char S_DOCTYPE_3[] = "\" \"".toCharArray();
33     private static final char S_DOCTYPE_4[] = " SYSTEM \"".toCharArray();
34     private static final char S_DOCTYPE_5[] = "\">".toCharArray();
35     private static final char S_DOCTYPE_6[] = ">".toCharArray();
36     
37     /** The name of the root element. */
38     protected String JavaDoc root_name = null;
39     /** The configured system identifier. */
40     protected String JavaDoc public_id = null;
41     /** The configured public identifier. */
42     protected String JavaDoc system_id = null;
43
44     /**
45      * Create a new <code>DocType</code> instance.
46      *
47      * @param root_name The document root element name.
48      */

49     public DocType(String JavaDoc root_name) {
50         this(root_name, null, null);
51     }
52     
53     /**
54      * Create a new <code>DocType</code> instance.
55      *
56      * @param root_name The document root element name.
57      * @param system_id The document type system identifier.
58      */

59     public DocType(String JavaDoc root_name, String JavaDoc system_id) {
60         this(root_name, null, system_id);
61     }
62
63     /**
64      * Create a new <code>DocType</code> instance.
65      *
66      * @param root_name The document root element name.
67      * @param public_id The document type public identifier.
68      * @param system_id The document type system identifier.
69      */

70     public DocType(String JavaDoc root_name, String JavaDoc public_id, String JavaDoc system_id) {
71         super();
72         if (root_name == null)
73             throw new NullPointerException JavaDoc("Invalid root document name");
74
75         if ((public_id != null) && (system_id == null))
76             throw new NullPointerException JavaDoc("Required System ID is NULL");
77
78         this.root_name = root_name;
79         this.public_id = public_id;
80         this.system_id = system_id;
81     }
82
83     /**
84      * Return the document root element name.
85      */

86     public String JavaDoc getName() {
87         return(this.root_name);
88     }
89
90     /**
91      * Return the document type public identifier or <b>null</b> if none
92      * configured..
93      */

94     public String JavaDoc getPublicId() {
95         return(this.public_id);
96     }
97
98     /**
99      * Return the document type system identifier or <b>null</b> if none
100      * configured..
101      */

102     public String JavaDoc getSystemId() {
103         return(this.system_id);
104     }
105
106     /**
107      * Return the document type declaration as a string
108      */

109     public String JavaDoc toString() {
110         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
111
112         buf.append(S_DOCTYPE_1); // [<!DOCTYPE ]
113
buf.append(this.root_name);
114         if (this.public_id != null) {
115             buf.append(S_DOCTYPE_2); // [ PUBLIC "]
116
buf.append(this.public_id);
117             /* This is wrong in XML, but not in SGML/HTML */
118             if (this.system_id != null) {
119                 buf.append(S_DOCTYPE_3); // [" "]
120
buf.append(this.system_id);
121             }
122             buf.append(S_DOCTYPE_5); // [">]
123
} else if (this.system_id != null) {
124             buf.append(S_DOCTYPE_4); // [ SYSTEM "]
125
buf.append(this.system_id);
126             buf.append(S_DOCTYPE_5); // [">]
127
} else {
128             buf.append(S_DOCTYPE_6); // [>]
129
}
130         return(buf.toString());
131     }
132
133     /**
134      * Check if the specified object is equal to this <code>DocType</code>
135      * instance.
136      */

137     public boolean equals(Object JavaDoc object) {
138         if (!(object instanceof DocType)) {
139             return false;
140         }
141         DocType rhs = (DocType)object;
142         return new EqualsBuilder()
143             .appendSuper(super.equals(object))
144             .append(public_id, rhs.public_id)
145             .append(system_id, rhs.system_id)
146             .append(root_name, rhs.root_name)
147             .isEquals();
148     }
149
150     /**
151      * Returns the hash code value for this docType
152      */

153     public int hashCode() {
154         return new HashCodeBuilder()
155             .appendSuper(super.hashCode())
156             .append(public_id)
157             .append(system_id)
158             .append(root_name)
159             .toHashCode();
160     }
161 }
162
Popular Tags