1 28 29 package com.caucho.xml; 30 31 import com.caucho.util.IntMap; 32 33 import java.io.IOException ; 34 35 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 entity) 58 { 59 return _entities.get(entity); 60 } 61 62 65 void printText(XmlPrinter os, 66 char []text, int offset, int length, 67 boolean attr) 68 throws IOException 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("<"); 91 97 break; 98 99 case '>': 100 os.print(">"); 101 107 break; 108 109 case '&': 110 os.print("&"); 111 break; 112 113 case '"': 114 if (attr) 115 os.print("""); 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 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 |