View Javadoc

1   /*
2    * Copyright 2001-2004 (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    * $Id: InternalEntityDecl.java,v 1.7 2004/06/25 08:03:36 maartenc Exp $
8    */
9   
10  package org.dom4j.dtd;
11  
12  /*** <p><code>InternalEntityDecl</code> represents an internal entity declaration in a DTD.</p>
13    *
14    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
15    * @version $Revision: 1.7 $
16    */
17  public class InternalEntityDecl {
18  
19      /*** Holds value of property name. */
20      private String name;
21      
22      /*** Holds value of property value. */
23      private String value;
24      
25      
26      public InternalEntityDecl() {
27      }
28  
29      public InternalEntityDecl(String name, String value) {
30          this.name = name;
31          this.value = value;
32      }
33  
34      /*** Getter for property name.
35       * @return Value of property name.
36       */
37      public String getName() {
38          return name;
39      }
40      
41      /*** Setter for property name.
42       * @param name New value of property name.
43       */
44      public void setName(String name) {
45          this.name = name;
46      }
47      
48      /*** Getter for property value.
49       * @return Value of property value.
50       */
51      public String getValue() {
52          return value;
53      }
54      
55      /*** Setter for property value.
56       * @param value New value of property value.
57       */
58      public void setValue(String value) {
59          this.value = value;
60      }
61      
62      public String toString() {
63          StringBuffer buffer = new StringBuffer( "<!ENTITY " );
64          
65          if (name.startsWith("%")) {
66              buffer.append("% ");
67              buffer.append(name.substring(1));
68          } 
69          else {
70              buffer.append(name);
71          }
72  
73          buffer.append(" \"");
74          buffer.append(escapeEntityValue(value));
75          buffer.append("\">");
76          
77          return buffer.toString();
78      }
79      
80      private String escapeEntityValue(String text) {
81          StringBuffer result = new StringBuffer();
82          for (int i = 0; i < text.length(); i++) {
83              char c = text.charAt(i);
84              switch (c) {
85                  case '<':
86                      result.append("&#38;#60;");
87                      break;
88                  case '>':
89                      result.append("&#62;");
90                      break;
91                  case '&':
92                      result.append("&#38;#38;");
93                      break;
94                  case '\'':
95                      result.append("&#39;");
96                      break;
97                  case '\"':
98                      result.append("&#34;");
99                      break;
100                 default:
101                     if (c < 32) {
102                         result.append("&#" + (int) c + ";");
103                     } else {
104                         result.append(c);
105                     }
106                     break;
107             }
108         }
109         
110         return result.toString();
111     }
112     
113 }
114 
115 
116 
117 
118 /*
119  * Redistribution and use of this software and associated documentation
120  * ("Software"), with or without modification, are permitted provided
121  * that the following conditions are met:
122  *
123  * 1. Redistributions of source code must retain copyright
124  *    statements and notices.  Redistributions must also contain a
125  *    copy of this document.
126  *
127  * 2. Redistributions in binary form must reproduce the
128  *    above copyright notice, this list of conditions and the
129  *    following disclaimer in the documentation and/or other
130  *    materials provided with the distribution.
131  *
132  * 3. The name "DOM4J" must not be used to endorse or promote
133  *    products derived from this Software without prior written
134  *    permission of MetaStuff, Ltd.  For written permission,
135  *    please contact dom4j-info@metastuff.com.
136  *
137  * 4. Products derived from this Software may not be called "DOM4J"
138  *    nor may "DOM4J" appear in their names without prior written
139  *    permission of MetaStuff, Ltd. DOM4J is a registered
140  *    trademark of MetaStuff, Ltd.
141  *
142  * 5. Due credit should be given to the DOM4J Project - 
143  *    http://www.dom4j.org
144  *
145  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
146  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
147  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
148  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
149  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
150  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
151  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
152  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
153  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
154  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
155  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
156  * OF THE POSSIBILITY OF SUCH DAMAGE.
157  *
158  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
159  *
160  * $Id: InternalEntityDecl.java,v 1.7 2004/06/25 08:03:36 maartenc Exp $
161  */