001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.serializers.util;
018:
019: import org.apache.commons.lang.builder.EqualsBuilder;
020: import org.apache.commons.lang.builder.HashCodeBuilder;
021:
022: /**
023: * The <code>DocType</code> class encapsulates informations regarding
024: * the document type public and system IDs and root element name.
025: *
026: * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>, February 2003
027: * @version CVS $Id: DocType.java 433543 2006-08-22 06:22:54Z crossley $
028: */
029: public class DocType {
030:
031: private static final char S_DOCTYPE_1[] = "<!DOCTYPE "
032: .toCharArray();
033: private static final char S_DOCTYPE_2[] = " PUBLIC \""
034: .toCharArray();
035: private static final char S_DOCTYPE_3[] = "\" \"".toCharArray();
036: private static final char S_DOCTYPE_4[] = " SYSTEM \""
037: .toCharArray();
038: private static final char S_DOCTYPE_5[] = "\">".toCharArray();
039: private static final char S_DOCTYPE_6[] = ">".toCharArray();
040:
041: /** The name of the root element. */
042: protected String root_name = null;
043: /** The configured system identifier. */
044: protected String public_id = null;
045: /** The configured public identifier. */
046: protected String system_id = null;
047:
048: /**
049: * Create a new <code>DocType</code> instance.
050: *
051: * @param root_name The document root element name.
052: */
053: public DocType(String root_name) {
054: this (root_name, null, null);
055: }
056:
057: /**
058: * Create a new <code>DocType</code> instance.
059: *
060: * @param root_name The document root element name.
061: * @param system_id The document type system identifier.
062: */
063: public DocType(String root_name, String system_id) {
064: this (root_name, null, system_id);
065: }
066:
067: /**
068: * Create a new <code>DocType</code> instance.
069: *
070: * @param root_name The document root element name.
071: * @param public_id The document type public identifier.
072: * @param system_id The document type system identifier.
073: */
074: public DocType(String root_name, String public_id, String system_id) {
075: super ();
076: if (root_name == null)
077: throw new NullPointerException("Invalid root document name");
078:
079: if ((public_id != null) && (system_id == null))
080: throw new NullPointerException("Required System ID is NULL");
081:
082: this .root_name = root_name;
083: this .public_id = public_id;
084: this .system_id = system_id;
085: }
086:
087: /**
088: * Return the document root element name.
089: */
090: public String getName() {
091: return (this .root_name);
092: }
093:
094: /**
095: * Return the document type public identifier or <b>null</b> if none
096: * configured..
097: */
098: public String getPublicId() {
099: return (this .public_id);
100: }
101:
102: /**
103: * Return the document type system identifier or <b>null</b> if none
104: * configured..
105: */
106: public String getSystemId() {
107: return (this .system_id);
108: }
109:
110: /**
111: * Return the document type declaration as a string
112: */
113: public String toString() {
114: StringBuffer buf = new StringBuffer();
115:
116: buf.append(S_DOCTYPE_1); // [<!DOCTYPE ]
117: buf.append(this .root_name);
118: if (this .public_id != null) {
119: buf.append(S_DOCTYPE_2); // [ PUBLIC "]
120: buf.append(this .public_id);
121: /* This is wrong in XML, but not in SGML/HTML */
122: if (this .system_id != null) {
123: buf.append(S_DOCTYPE_3); // [" "]
124: buf.append(this .system_id);
125: }
126: buf.append(S_DOCTYPE_5); // [">]
127: } else if (this .system_id != null) {
128: buf.append(S_DOCTYPE_4); // [ SYSTEM "]
129: buf.append(this .system_id);
130: buf.append(S_DOCTYPE_5); // [">]
131: } else {
132: buf.append(S_DOCTYPE_6); // [>]
133: }
134: return (buf.toString());
135: }
136:
137: /**
138: * Check if the specified object is equal to this <code>DocType</code>
139: * instance.
140: */
141: public boolean equals(Object object) {
142: if (!(object instanceof DocType)) {
143: return false;
144: }
145: DocType rhs = (DocType) object;
146: return new EqualsBuilder().appendSuper(super .equals(object))
147: .append(public_id, rhs.public_id).append(system_id,
148: rhs.system_id).append(root_name, rhs.root_name)
149: .isEquals();
150: }
151:
152: /**
153: * Returns the hash code value for this docType
154: */
155: public int hashCode() {
156: return new HashCodeBuilder().appendSuper(super.hashCode())
157: .append(public_id).append(system_id).append(root_name)
158: .toHashCode();
159: }
160: }
|