Changeset 25658
- Timestamp:
- 2012-05-23T16:55:45+12:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java
r25635 r25658 1103 1103 { 1104 1104 StringBuffer sb = new StringBuffer(""); 1105 xmlNodeToString(sb, e, 0, true);1105 xmlNodeToString(sb, e, true, "\t", 0); 1106 1106 return sb.toString(); 1107 1107 } 1108 1108 1109 public static String xmlNodeToString(Node e, boolean printText) 1110 { 1111 StringBuffer sb = new StringBuffer(""); 1112 xmlNodeToString(sb, e, 0, printText); 1113 return sb.toString(); 1114 } 1115 1116 private static void xmlNodeToString(StringBuffer sb, Node e, int depth, boolean printText) 1117 { 1118 if (e == null) 1119 { 1109 public static void xmlNodeToString(StringBuffer sb, Node e, boolean indent, String indentString, int depth) 1110 { 1111 if (e.getNodeType() == Node.TEXT_NODE) 1112 { 1113 if (e.getNodeValue() != "") 1114 { 1115 String text = e.getNodeValue(); 1116 text = text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("^[\\n\\r\\t\\s]*", "").replaceAll("[\\n\\r\\t\\s]*$", ""); 1117 sb.append(text); 1118 } 1120 1119 return; 1121 1120 } 1122 1121 1123 for (int i = 0; i < depth; i++) 1124 sb.append(' '); 1125 1126 if (e.getNodeType() == Node.TEXT_NODE) 1127 { 1128 if (printText) 1129 { 1130 sb.append(e.getNodeValue()); 1131 } 1132 else 1133 { 1134 sb.append("text"); 1122 if (e.getNodeType() == Node.COMMENT_NODE) 1123 { 1124 if (e.getNodeValue() != "") 1125 { 1126 sb.append("<!--" + e.getNodeValue() + "-->"); 1135 1127 } 1136 1128 return; 1129 } 1130 1131 if (indent) 1132 { 1133 for (int i = 0; i < depth; i++) 1134 { 1135 sb.append(indentString); 1136 } 1137 1137 } 1138 1138 … … 1148 1148 sb.append(attr.getNodeName()); 1149 1149 sb.append("=\""); 1150 sb.append(attr.getNodeValue() );1150 sb.append(attr.getNodeValue().replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")); 1151 1151 sb.append('"'); 1152 1152 } 1153 1153 } 1154 1155 1154 NodeList children = e.getChildNodes(); 1156 1155 1156 boolean hasElements = false; 1157 boolean indentSwapped = false; 1158 for (int i = 0; i < children.getLength(); i++) 1159 { 1160 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 1161 { 1162 hasElements = true; 1163 } 1164 if (children.item(i).getNodeType() == Node.TEXT_NODE && indent) 1165 { 1166 if (children.item(i).getNodeValue().trim().length() > 0) 1167 { 1168 indentSwapped = true; 1169 indent = false; 1170 } 1171 } 1172 } 1173 1157 1174 if (children == null || children.getLength() == 0) 1158 sb.append("/>\n"); 1175 { 1176 sb.append("/>"); 1177 1178 if (indent) 1179 { 1180 sb.append("\n"); 1181 } 1182 } 1159 1183 else 1160 1184 { 1161 1162 sb.append(">\n"); 1185 sb.append(">"); 1186 if (hasElements && indent) 1187 { 1188 sb.append("\n"); 1189 } 1163 1190 1164 1191 int len = children.getLength(); 1165 1192 for (int i = 0; i < len; i++) 1166 1193 { 1167 xmlNodeToString(sb, children.item(i), depth + 1, printText); 1168 } 1169 1170 for (int i = 0; i < depth; i++) 1171 sb.append(' '); 1172 1173 sb.append("</" + e.getNodeName() + ">\n"); 1174 } 1175 1194 xmlNodeToString(sb, children.item(i), indent, indentString, depth + 1); 1195 } 1196 1197 if (indent) 1198 { 1199 for (int i = 0; i < depth; i++) 1200 { 1201 sb.append(indentString); 1202 } 1203 } 1204 1205 sb.append("</" + e.getNodeName() + ">"); 1206 1207 if ((hasElements && indent) || indentSwapped) 1208 { 1209 sb.append("\n"); 1210 } 1211 } 1176 1212 } 1177 1213 … … 1239 1275 } 1240 1276 1241 public static void elementToLogAsString(Element e, boolean indent) 1242 { 1243 String str = elementToString(e, indent); 1244 System.err.println(str); 1245 logger.error(str); 1246 } 1247 1248 public static String elementToString(Element e, boolean indent) 1249 { 1250 String str = ""; 1251 try { 1252 TransformerFactory tf = TransformerFactory.newInstance(); 1253 Transformer trans = tf.newTransformer(); 1254 StringWriter sw = new StringWriter(); 1255 if(indent) { 1256 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 1257 } else { 1258 trans.setOutputProperty(OutputKeys.INDENT, "no"); 1259 } 1260 trans.transform(new DOMSource(e), new StreamResult(sw)); 1261 str += sw.toString(); 1262 } 1263 catch (Exception ex) { 1264 str += "Exception: couldn't write " + e + " to log"; 1265 } 1266 finally { 1267 return str; 1268 } 1277 public static void elementToLogAsString(Element e, boolean indent) 1278 { 1279 String str = elementToString(e, indent); 1280 System.err.println(str); 1281 logger.error(str); 1282 } 1283 1284 public static String elementToString(Element e, boolean indent) 1285 { 1286 String str = ""; 1287 try 1288 { 1289 TransformerFactory tf = TransformerFactory.newInstance(); 1290 Transformer trans = tf.newTransformer(); 1291 StringWriter sw = new StringWriter(); 1292 if (indent) 1293 { 1294 trans.setOutputProperty(OutputKeys.INDENT, "yes"); 1295 } 1296 else 1297 { 1298 trans.setOutputProperty(OutputKeys.INDENT, "no"); 1299 } 1300 trans.transform(new DOMSource(e), new StreamResult(sw)); 1301 str += sw.toString(); 1302 } 1303 catch (Exception ex) 1304 { 1305 str += "Exception: couldn't write " + e + " to log"; 1306 } 1307 finally 1308 { 1309 return str; 1310 } 1269 1311 } 1270 1312 … … 1289 1331 return groups; 1290 1332 } 1333 1334 public static NodeList getHTMLStructureElements(Document doc) 1335 { 1336 MyNodeList elems = new MyNodeList(); 1337 1338 String[] structureTagNames = new String[] { "html", "div", "td", "li" }; 1339 1340 for (String tagName : structureTagNames) 1341 { 1342 NodeList htmlElems = doc.getElementsByTagName(tagName); 1343 elems.addNodeList(htmlElems); 1344 } 1345 1346 return elems; 1347 } 1348 1349 public static void addDebugSpanTags(Document doc) 1350 { 1351 NodeList allElements = doc.getElementsByTagName("*"); 1352 1353 for (int i = 0; i < allElements.getLength(); i++) 1354 { 1355 Element current = (Element) allElements.item(i); 1356 String debugString = null; 1357 if ((debugString = (String) current.getUserData("GSDEBUGFILENAME")) != null) 1358 { 1359 System.err.println("DEBUGSTRING = " + debugString); 1360 1361 Element xmlSpan = doc.createElement("span"); 1362 xmlSpan.setAttribute("style", "display:none;"); 1363 xmlSpan.setAttribute("class", "debugSpan"); 1364 xmlSpan.appendChild(doc.createTextNode("\"filename\":\"" + debugString + "\", \"xml\":\"" + GSXML.xmlNodeToString((Element) current.getUserData("GSDEBUGXML")) + "\"")); 1365 1366 if (current.hasChildNodes()) 1367 { 1368 current.insertBefore(xmlSpan, current.getFirstChild()); 1369 } 1370 else 1371 { 1372 current.appendChild(xmlSpan); 1373 } 1374 } 1375 } 1376 } 1291 1377 }
Note:
See TracChangeset
for help on using the changeset viewer.