KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > dtd > InternalEntityDecl


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  */

7
8 package org.dom4j.dtd;
9
10 /**
11  * <p>
12  * <code>InternalEntityDecl</code> represents an internal entity declaration
13  * in a DTD.
14  * </p>
15  *
16  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
17  * @version $Revision: 1.9 $
18  */

19 public class InternalEntityDecl {
20     /** Holds value of property name. */
21     private String JavaDoc name;
22
23     /** Holds value of property value. */
24     private String JavaDoc value;
25
26     public InternalEntityDecl() {
27     }
28
29     public InternalEntityDecl(String JavaDoc name, String JavaDoc value) {
30         this.name = name;
31         this.value = value;
32     }
33
34     /**
35      * Getter for property name.
36      *
37      * @return Value of property name.
38      */

39     public String JavaDoc getName() {
40         return name;
41     }
42
43     /**
44      * Setter for property name.
45      *
46      * @param name
47      * New value of property name.
48      */

49     public void setName(String JavaDoc name) {
50         this.name = name;
51     }
52
53     /**
54      * Getter for property value.
55      *
56      * @return Value of property value.
57      */

58     public String JavaDoc getValue() {
59         return value;
60     }
61
62     /**
63      * Setter for property value.
64      *
65      * @param value
66      * New value of property value.
67      */

68     public void setValue(String JavaDoc value) {
69         this.value = value;
70     }
71
72     public String JavaDoc toString() {
73         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("<!ENTITY ");
74
75         if (name.startsWith("%")) {
76             buffer.append("% ");
77             buffer.append(name.substring(1));
78         } else {
79             buffer.append(name);
80         }
81
82         buffer.append(" \"");
83         buffer.append(escapeEntityValue(value));
84         buffer.append("\">");
85
86         return buffer.toString();
87     }
88
89     private String JavaDoc escapeEntityValue(String JavaDoc text) {
90         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
91
92         for (int i = 0; i < text.length(); i++) {
93             char c = text.charAt(i);
94
95             switch (c) {
96                 case '<':
97                     result.append("&#38;#60;");
98
99                     break;
100
101                 case '>':
102                     result.append("&#62;");
103
104                     break;
105
106                 case '&':
107                     result.append("&#38;#38;");
108
109                     break;
110
111                 case '\'':
112                     result.append("&#39;");
113
114                     break;
115
116                 case '\"':
117                     result.append("&#34;");
118
119                     break;
120
121                 default:
122
123                     if (c < 32) {
124                         result.append("&#" + (int) c + ";");
125                     } else {
126                         result.append(c);
127                     }
128
129                     break;
130             }
131         }
132
133         return result.toString();
134     }
135 }
136
137 /*
138  * Redistribution and use of this software and associated documentation
139  * ("Software"), with or without modification, are permitted provided that the
140  * following conditions are met:
141  *
142  * 1. Redistributions of source code must retain copyright statements and
143  * notices. Redistributions must also contain a copy of this document.
144  *
145  * 2. Redistributions in binary form must reproduce the above copyright notice,
146  * this list of conditions and the following disclaimer in the documentation
147  * and/or other materials provided with the distribution.
148  *
149  * 3. The name "DOM4J" must not be used to endorse or promote products derived
150  * from this Software without prior written permission of MetaStuff, Ltd. For
151  * written permission, please contact dom4j-info@metastuff.com.
152  *
153  * 4. Products derived from this Software may not be called "DOM4J" nor may
154  * "DOM4J" appear in their names without prior written permission of MetaStuff,
155  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
156  *
157  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
158  *
159  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
160  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
162  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
163  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
164  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
165  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
166  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
167  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
168  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
169  * POSSIBILITY OF SUCH DAMAGE.
170  *
171  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
172  */

173
Popular Tags