View Javadoc

1   package com.wutka.dtd;
2   
3   import java.io.*;
4   import java.net.*;
5   
6   /*** Represents an Entity defined in a DTD
7    *
8    * @author Mark Wutka
9    * @version $Revision: 1.18 $ $Date: 2002/07/31 00:19:10 $ by $Author: wutka $
10   */
11  public class DTDEntity implements DTDOutput
12  {
13      public String name;
14      public boolean isParsed;
15      public String value;
16      public DTDExternalID externalID;
17      public String ndata;
18      public Object defaultLocation;
19  
20      public DTDEntity()
21      {
22      }
23  
24      public DTDEntity(String aName)
25      {
26          name = aName;
27      }
28  
29      public DTDEntity(String aName, Object aDefaultLocation)
30      {
31          name = aName;
32          defaultLocation = aDefaultLocation;
33      }
34  
35  /*** Writes out an entity declaration for this entity */
36      public void write(PrintWriter out)
37          throws IOException
38      {
39          out.print("<!ENTITY ");
40          if (isParsed)
41          {
42              out.print(" % ");
43          }
44          out.print(name);
45  
46          if (value != null)
47          {
48              char quoteChar = '"';
49              if (value.indexOf(quoteChar) >= 0) quoteChar='\'';
50              out.print(quoteChar);
51              out.print(value);
52              out.print(quoteChar);
53          }
54          else
55          {
56              externalID.write(out);
57              if (ndata != null)
58              {
59                  out.print(" NDATA ");
60                  out.print(ndata);
61              }
62          }
63          out.println(">");
64      }
65  
66      public String getExternalId()
67      {
68          return(externalID.system);
69      }
70  
71      public Reader getReader()
72          throws IOException
73      {
74          // MAW Ver 1.19 - Added check for externalID == null
75          if (externalID == null)
76          {
77              return null;
78          }
79  
80          Reader rd = getReader(externalID.system);
81  
82          return rd;
83      }
84  
85      public Reader getReader(String entityName)
86      {
87          try
88          {
89              if (defaultLocation != null)
90              {
91                  if (defaultLocation instanceof File)
92                  {
93                      File loc = (File) defaultLocation;
94  
95                      BufferedReader in = new BufferedReader(
96                          new FileReader(new File(loc, entityName)));
97  
98                      return in;
99                  }
100                 else if (defaultLocation instanceof URL)
101                 {
102                     // MAW Version 1.17
103                     // Changed to construct new URL based on default
104                     // location plus the entity name just like is done
105                     // with the File-based name. This allows parsing of
106                     // a URL-based DTD file that references other files either
107                     // relatively or absolutely.
108                     URL url = new URL((URL) defaultLocation, entityName);
109 
110                     BufferedReader in = new BufferedReader(
111                         new InputStreamReader(url.openStream()));
112 
113                     return in;
114                 }
115             }
116             BufferedReader in = new BufferedReader(
117                 new FileReader(entityName));
118 
119             return in;
120         }
121         catch (Exception ignore)
122         {
123         }
124 
125         try
126         {
127             URL url = new URL(entityName);
128 
129             InputStream inStream = url.openStream();
130 
131             BufferedReader in = new BufferedReader(
132                 new InputStreamReader(inStream));
133 
134             return in;
135         }
136         catch (Exception ignore)
137         {
138         }
139 
140         return null;
141     }
142 
143     public boolean equals(Object ob)
144     {
145         if (ob == this) return true;
146         if (!(ob instanceof DTDEntity)) return false;
147 
148         DTDEntity other = (DTDEntity) ob;
149 
150         if (name == null)
151         {
152             if (other.name != null) return false;
153         }
154         else
155         {
156             if (!name.equals(other.name)) return false;
157         }
158 
159         if (isParsed != other.isParsed) return false;
160 
161 
162         if (value == null)
163         {
164             if (other.value != null) return false;
165         }
166         else
167         {
168             if (!value.equals(other.value)) return false;
169         }
170 
171         if (externalID == null)
172         {
173             if (other.externalID != null) return false;
174         }
175         else
176         {
177             if (!externalID.equals(other.externalID)) return false;
178         }
179 
180         if (ndata == null)
181         {
182             if (other.ndata != null) return false;
183         }
184         else
185         {
186             if (!ndata.equals(other.ndata)) return false;
187         }
188 
189         return true;
190     }
191 
192 /*** Sets the name of this entity */
193     public void setName(String aName)
194     {
195         name = aName;
196     }
197 
198 /*** Returns the name of this entity */
199     public String getName()
200     {
201         return name;
202     }
203 
204 /*** Sets the isParsed flag */
205     public void setIsParsed(boolean flag)
206     {
207         isParsed = flag;
208     }
209 
210 /*** Returns the isParsed flag */
211     public boolean isParsed()
212     {
213         return isParsed;
214     }
215 
216 /*** Sets the entity value */
217     public void setValue(String aValue)
218     {
219         value = aValue;
220     }
221 
222 /*** Returns the entity value */
223     public String getValue()
224     {
225         return value;
226     }
227 
228 /*** Sets the external ID for the entity */
229     public void setExternalID(DTDExternalID anExternalID)
230     {
231         externalID = anExternalID;
232     }
233 
234 /*** Returns the external ID for the entity */
235     public DTDExternalID getExternalID()
236     {
237         return externalID;
238     }
239 
240 /*** Sets the entity ndata */
241     public void setNdata(String anNdata)
242     {
243         ndata = anNdata;
244     }
245 
246 /*** Returns the entity ndata */
247     public String getNdata()
248     {
249         return ndata;
250     }
251 }