001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import java.io.IOException;
032:
033: /**
034: * Encodings other than ascii and latin-1 try to write the characters in
035: * the given encoding. If the translation fails, then use a character
036: * encoding.
037: */
038: class OtherEntities extends HtmlEntities {
039: private static Entities _html40;
040: private static Entities _html32;
041:
042: static Entities create(double version) {
043: if (version == 0 || version >= 4.0) {
044: if (_html40 == null)
045: _html40 = new OtherEntities(4.0);
046:
047: return _html40;
048: } else {
049: if (_html32 == null)
050: _html32 = new OtherEntities(3.2);
051:
052: return _html32;
053: }
054: }
055:
056: protected OtherEntities(double version) {
057: super (version);
058: }
059:
060: void printText(XmlPrinter os, char[] text, int offset, int length,
061: boolean attr) throws IOException {
062: for (int i = 0; i < length; i++) {
063: char ch = text[i + offset];
064:
065: // ASCII codes use the standard escapes
066: if (ch == '&') {
067: if (i + 1 < length && text[i + 1] == '{')
068: os.print('&');
069: else if (attr)
070: os.print(_attrLatin1[ch]);
071: else
072: os.print(_latin1[ch]);
073: } else if (ch == '"') {
074: if (attr)
075: os.print(""");
076: else
077: os.print('"');
078: } else if (ch == '<') {
079: if (attr)
080: os.print('<');
081: else
082: os.print("<");
083: } else if (ch == '>') {
084: if (attr)
085: os.print('>');
086: else
087: os.print(">");
088: } else if (ch < 161)
089: os.print(_latin1[ch]);
090: else {
091: try {
092: os.print(ch);
093: } catch (IOException e) {
094: char[] value = getSparseEntity(ch);
095: if (value != null) {
096: os.print(value);
097: } else {
098: os.print("&#");
099: os.print((int) ch);
100: os.print(";");
101: }
102: }
103: }
104: }
105: }
106: }
|