Changeset 25658


Ignore:
Timestamp:
2012-05-23T16:55:45+12:00 (12 years ago)
Author:
sjm84
Message:

Bought over the xmlNodeToString function from XMLTools (GLI) and also added functions to help with the on-page xml viewing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r25635 r25658  
    11031103    {
    11041104        StringBuffer sb = new StringBuffer("");
    1105         xmlNodeToString(sb, e, 0, true);
     1105        xmlNodeToString(sb, e, true, "\t", 0);
    11061106        return sb.toString();
    11071107    }
    11081108
    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("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("^[\\n\\r\\t\\s]*", "").replaceAll("[\\n\\r\\t\\s]*$", "");
     1117                sb.append(text);
     1118            }
    11201119            return;
    11211120        }
    11221121
    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() + "-->");
    11351127            }
    11361128            return;
     1129        }
     1130
     1131        if (indent)
     1132        {
     1133            for (int i = 0; i < depth; i++)
     1134            {
     1135                sb.append(indentString);
     1136            }
    11371137        }
    11381138
     
    11481148                sb.append(attr.getNodeName());
    11491149                sb.append("=\"");
    1150                 sb.append(attr.getNodeValue());
     1150                sb.append(attr.getNodeValue().replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
    11511151                sb.append('"');
    11521152            }
    11531153        }
    1154 
    11551154        NodeList children = e.getChildNodes();
    11561155
     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
    11571174        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        }
    11591183        else
    11601184        {
    1161 
    1162             sb.append(">\n");
     1185            sb.append(">");
     1186            if (hasElements && indent)
     1187            {
     1188                sb.append("\n");
     1189            }
    11631190
    11641191            int len = children.getLength();
    11651192            for (int i = 0; i < len; i++)
    11661193            {
    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        }
    11761212    }
    11771213
     
    12391275    }
    12401276
    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        }
    12691311    }
    12701312
     
    12891331        return groups;
    12901332    }
     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    }
    12911377}
Note: See TracChangeset for help on using the changeset viewer.