1
2
3
4
5
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 name;
22
23 /*** Holds value of property value. */
24 private String value;
25
26 public InternalEntityDecl() {
27 }
28
29 public InternalEntityDecl(String name, String 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 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 name) {
50 this.name = name;
51 }
52
53 /***
54 * Getter for property value.
55 *
56 * @return Value of property value.
57 */
58 public String 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 value) {
69 this.value = value;
70 }
71
72 public String toString() {
73 StringBuffer buffer = new StringBuffer("<!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 escapeEntityValue(String text) {
90 StringBuffer result = new StringBuffer();
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("&#60;");
98
99 break;
100
101 case '>':
102 result.append(">");
103
104 break;
105
106 case '&':
107 result.append("&#38;");
108
109 break;
110
111 case '\'':
112 result.append("'");
113
114 break;
115
116 case '\"':
117 result.append(""");
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172