KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
32
33 /**
34  * Encodings other than ascii and latin-1 try to write the characters in
35  * the given encoding. If the translation fails, then use a character
36  * encoding.
37  */

38 class OtherEntities extends HtmlEntities {
39   private static Entities _html40;
40   private static Entities _html32;
41
42   static Entities create(double version)
43   {
44     if (version == 0 || version >= 4.0) {
45       if (_html40 == null)
46     _html40 = new OtherEntities(4.0);
47       
48       return _html40;
49     }
50     else {
51       if (_html32 == null)
52     _html32 = new OtherEntities(3.2);
53       
54       return _html32;
55     }
56   }
57
58   protected OtherEntities(double version)
59   {
60     super(version);
61   }
62
63   void printText(XmlPrinter os,
64                  char []text, int offset, int length,
65                  boolean attr)
66     throws IOException JavaDoc
67   {
68     for (int i = 0; i < length; i++) {
69       char ch = text[i + offset];
70
71       // ASCII codes use the standard escapes
72
if (ch == '&') {
73     if (i + 1 < length && text[i + 1] == '{')
74       os.print('&');
75     else if (attr)
76       os.print(_attrLatin1[ch]);
77         else
78       os.print(_latin1[ch]);
79       }
80       else if (ch == '"') {
81     if (attr)
82       os.print("&quot;");
83     else
84       os.print('"');
85       }
86       else if (ch == '<') {
87     if (attr)
88       os.print('<');
89     else
90       os.print("&lt;");
91       }
92       else if (ch == '>') {
93     if (attr)
94       os.print('>');
95     else
96       os.print("&gt;");
97       }
98       else if (ch < 161)
99     os.print(_latin1[ch]);
100       else {
101     try {
102       os.print(ch);
103     } catch (IOException JavaDoc e) {
104       char []value = getSparseEntity(ch);
105       if (value != null) {
106         os.print(value);
107       } else {
108         os.print("&#");
109         os.print((int) ch);
110         os.print(";");
111       }
112     }
113       }
114     }
115   }
116 }
117
Popular Tags