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 com.caucho.util.IntMap;
032:
033: import java.io.IOException;
034:
035: /**
036: * Standard XML entities.
037: */
038: class XmlEntities extends Entities {
039: private static Entities _xmlEntities = new XmlEntities();
040: private IntMap _entities;
041:
042: static Entities create() {
043: return _xmlEntities;
044: }
045:
046: protected XmlEntities() {
047: _entities = new IntMap();
048: _entities.put("lt", '<');
049: _entities.put("gt", '>');
050: _entities.put("amp", '&');
051: _entities.put("quot", '"');
052: _entities.put("apos", '\'');
053: }
054:
055: int getEntity(String entity) {
056: return _entities.get(entity);
057: }
058:
059: /**
060: * Print some text, replacing entities.
061: */
062: void printText(XmlPrinter os, char[] text, int offset, int length,
063: boolean attr) throws IOException {
064: for (int i = 0; i < length; i++) {
065: char ch = text[offset + i];
066:
067: switch (ch) {
068: case '\t':
069: os.print(ch);
070: break;
071:
072: case '\n':
073: case '\r':
074: if (!attr)
075: os.print(ch);
076: else {
077: os.print("&#");
078: os.print((int) ch);
079: os.print(";");
080: }
081: break;
082:
083: case '<':
084: os.print("<");
085: /*
086: if (! attr)
087: os.print("<");
088: else
089: os.print("<");
090: */
091: break;
092:
093: case '>':
094: os.print(">");
095: /*
096: if (! attr)
097: os.print(">");
098: else
099: os.print("<");
100: */
101: break;
102:
103: case '&':
104: os.print("&");
105: break;
106:
107: case '"':
108: if (attr)
109: os.print(""");
110: else
111: os.print('"');
112: break;
113:
114: default:
115: if (ch >= 0x20 && ch < 0x7f || XmlChar.isChar(ch)) {
116: try {
117: os.print(ch);
118: } catch (IOException e) {
119: os.print("&#");
120: os.print((int) ch);
121: os.print(';');
122: }
123: } else {
124: os.print("&#");
125: os.print((int) ch);
126: os.print(';');
127: }
128: }
129: }
130: }
131: }
|