KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > XmlEntities


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.IntMap;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Standard XML entities.
37  */

38 class XmlEntities extends Entities {
39   private static Entities _xmlEntities = new XmlEntities();
40   private IntMap _entities;
41
42   static Entities create()
43   {
44     return _xmlEntities;
45   }
46
47   protected XmlEntities()
48   {
49     _entities = new IntMap();
50     _entities.put("lt", '<');
51     _entities.put("gt", '>');
52     _entities.put("amp", '&');
53     _entities.put("quot", '"');
54     _entities.put("apos", '\'');
55   }
56
57   int getEntity(String JavaDoc entity)
58   {
59     return _entities.get(entity);
60   }
61
62   /**
63    * Print some text, replacing entities.
64    */

65   void printText(XmlPrinter os,
66                  char []text, int offset, int length,
67                  boolean attr)
68     throws IOException JavaDoc
69   {
70     for (int i = 0; i < length; i++) {
71       char ch = text[offset + i];
72
73       switch (ch) {
74       case '\t':
75     os.print(ch);
76         break;
77         
78       case '\n':
79       case '\r':
80         if (! attr)
81           os.print(ch);
82         else {
83           os.print("&#");
84           os.print((int) ch);
85           os.print(";");
86         }
87         break;
88
89       case '<':
90       os.print("&lt;");
91           /*
92     if (! attr)
93       os.print("&lt;");
94     else
95       os.print("<");
96           */

97     break;
98
99       case '>':
100       os.print("&gt;");
101           /*
102     if (! attr)
103       os.print("&gt;");
104     else
105       os.print("<");
106           */

107     break;
108         
109       case '&':
110         os.print("&amp;");
111         break;
112         
113       case '"':
114     if (attr)
115       os.print("&quot;");
116     else
117       os.print('"');
118     break;
119
120       default:
121     if (ch >= 0x20 && ch < 0x7f || XmlChar.isChar(ch)) {
122       try {
123         os.print(ch);
124       } catch (IOException JavaDoc e) {
125         os.print("&#");
126         os.print((int) ch);
127         os.print(';');
128       }
129     }
130     else {
131       os.print("&#");
132       os.print((int) ch);
133       os.print(';');
134     }
135       }
136     }
137   }
138 }
139
Popular Tags