- Timestamp:
- 2012-03-12T10:51:09+13:00 (12 years ago)
- Location:
- main/trunk/gli/src/org/greenstone/gatherer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/gli/src/org/greenstone/gatherer/cdm/CollectionConfigXMLReadWrite.java
r24931 r25206 639 639 if (format != null && format.hasChildNodes()) 640 640 { // not an empty format statement 641 String gsf_text = XMLTools.xmlNodeToString WithoutIndenting(format);641 String gsf_text = XMLTools.xmlNodeToString(format); 642 642 643 643 //We don't want the <format> elements in the string … … 2366 2366 static public void save(File collect_config_xml_file, Document doc) 2367 2367 { 2368 2369 2368 Document collection_config_xml_document = convertInternalToCollectionConfig(doc); 2370 2369 String[] nonEscapingTagNames = { StaticStrings.FORMAT_STR, StaticStrings.DISPLAYITEM_STR }; 2371 2370 XMLTools.writeXMLFile(collect_config_xml_file, collection_config_xml_document, nonEscapingTagNames); 2372 2373 2371 } 2374 2372 -
main/trunk/gli/src/org/greenstone/gatherer/util/XMLTools.java
r25165 r25206 712 712 xml.append("\n"); 713 713 } 714 else { 715 System.err.println(line); 714 else 715 { 716 System.err.println(line); 716 717 } 717 718 } … … 791 792 } 792 793 794 static public void indentXML(Element elem, int depth) 795 { 796 Document doc = elem.getOwnerDocument(); 797 798 String startIndentString = "\n"; 799 for (int i = 0; i < depth; i++) 800 { 801 startIndentString += "\t"; 802 } 803 Node startTextNode = doc.createTextNode(startIndentString); 804 805 String endIndentString = "\n"; 806 for (int i = 0; i < depth - 1; i++) 807 { 808 endIndentString += "\t"; 809 } 810 Node endTextNode = doc.createTextNode(endIndentString); 811 812 boolean found = false; 813 Node child = elem.getFirstChild(); 814 while (child != null) 815 { 816 if (child.getNodeType() == Node.ELEMENT_NODE) 817 { 818 found = true; 819 break; 820 } 821 child = child.getNextSibling(); 822 } 823 824 if (found) 825 { 826 elem.appendChild(endTextNode); 827 } 828 829 child = elem.getFirstChild(); 830 while (child != null) 831 { 832 if (child.getNodeType() == Node.ELEMENT_NODE) 833 { 834 elem.insertBefore(startTextNode.cloneNode(false), child); 835 indentXML((Element) child, depth + 1); 836 } 837 child = child.getNextSibling(); 838 } 839 } 840 793 841 /** 794 842 * Write an XML document to a given file with the text node of the specified … … 797 845 static public void writeXMLFile(File xml_file, Document document, String[] nonEscapingTagNames) 798 846 { 847 indentXML(document.getDocumentElement(), 1); 799 848 try 800 849 { … … 895 944 { 896 945 StringBuffer sb = new StringBuffer(""); 897 xmlNodeToString(sb, e, 0);946 xmlNodeToString(sb, e, true, 2); 898 947 return sb.toString(); 899 948 } 900 949 901 private static void xmlNodeToString(StringBuffer sb, Node e, int depth) 950 private static void xmlNodeToString(StringBuffer sb, Node e, boolean indent, int depth) 951 { 952 if (e.getNodeType() == Node.TEXT_NODE) 953 { 954 if (e.getNodeValue() != "") 955 { 956 String text = e.getNodeValue(); 957 text = text.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("^[\\n\\r\\t\\s]*", ""); 958 sb.append(text); 959 } 960 return; 961 } 962 963 if (e.getNodeType() == Node.COMMENT_NODE) 964 { 965 if (e.getNodeValue() != "") 966 { 967 sb.append("<!--" + e.getNodeValue() + "-->"); 968 } 969 return; 970 } 971 972 if (indent) 973 { 974 for (int i = 0; i < depth; i++) 975 { 976 sb.append('\t'); 977 } 978 } 979 980 sb.append('<'); 981 sb.append(e.getNodeName()); 982 NamedNodeMap attrs = e.getAttributes(); 983 if (attrs != null) 984 { 985 for (int i = 0; i < attrs.getLength(); i++) 986 { 987 Node attr = attrs.item(i); 988 sb.append(' '); 989 sb.append(attr.getNodeName()); 990 sb.append("=\""); 991 sb.append(attr.getNodeValue().replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")); 992 sb.append('"'); 993 } 994 } 995 NodeList children = e.getChildNodes(); 996 997 boolean hasElements = false; 998 boolean indentSwapped = false; 999 for (int i = 0; i < children.getLength(); i++) 1000 { 1001 if (children.item(i).getNodeType() == Node.ELEMENT_NODE) 1002 { 1003 hasElements = true; 1004 } 1005 if (children.item(i).getNodeType() == Node.TEXT_NODE && indent) 1006 { 1007 if (children.item(i).getNodeValue().matches("[^\\s]*")) 1008 { 1009 indentSwapped = true; 1010 indent = false; 1011 } 1012 } 1013 } 1014 1015 if (children == null || children.getLength() == 0) 1016 { 1017 sb.append("/>"); 1018 1019 if (indent) 1020 { 1021 sb.append("\n"); 1022 } 1023 } 1024 else 1025 { 1026 sb.append(">"); 1027 if (hasElements && indent) 1028 { 1029 sb.append("\n"); 1030 } 1031 1032 int len = children.getLength(); 1033 for (int i = 0; i < len; i++) 1034 { 1035 xmlNodeToString(sb, children.item(i), indent, depth + 1); 1036 } 1037 1038 if (indent) 1039 { 1040 for (int i = 0; i < depth; i++) 1041 { 1042 sb.append('\t'); 1043 } 1044 } 1045 1046 sb.append("</" + e.getNodeName() + ">"); 1047 1048 if ((hasElements && indent) || indentSwapped) 1049 { 1050 sb.append("\n"); 1051 } 1052 } 1053 } 1054 1055 public static String xmlNodeToStringWithoutIndenting(Node e) 1056 { 1057 StringBuffer sb = new StringBuffer(""); 1058 xmlNodeToStringWithoutNewline(sb, e, -1); 1059 return sb.toString(); 1060 } 1061 1062 public static String xmlNodeToStringWithoutNewline(Node e) 1063 { 1064 StringBuffer sb = new StringBuffer(""); 1065 xmlNodeToStringWithoutNewline(sb, e, 0); 1066 return sb.toString(); 1067 } 1068 1069 private static void xmlNodeToStringWithoutNewline(StringBuffer sb, Node e, int depth) 902 1070 { 903 1071 904 1072 for (int i = 0; i < depth; i++) 1073 { 905 1074 sb.append(' '); 1075 } 906 1076 907 1077 if (e.getNodeType() == Node.TEXT_NODE) … … 909 1079 if (e.getNodeValue() != "") 910 1080 { 911 sb.append(e.getNodeValue() );1081 sb.append(e.getNodeValue().replaceAll("&", "&").replaceAll("<", "<").replace(">", ">")); 912 1082 } 913 1083 return; 914 1084 } 915 1085 916 1086 if (e.getNodeType() == Node.COMMENT_NODE) 917 1087 { … … 941 1111 942 1112 if (children == null || children.getLength() == 0) 943 sb.append("/> \n");1113 sb.append("/>"); 944 1114 else 945 1115 { 946 1116 947 sb.append("> \n");1117 sb.append(">"); 948 1118 949 1119 int len = children.getLength(); 950 1120 for (int i = 0; i < len; i++) 951 1121 { 952 xmlNodeToString(sb, children.item(i), depth + 1); 1122 if (depth >= 0) 1123 { 1124 xmlNodeToStringWithoutNewline(sb, children.item(i), depth + 1); 1125 } 1126 else 1127 { 1128 xmlNodeToStringWithoutNewline(sb, children.item(i), depth); 1129 } 953 1130 } 954 1131 … … 956 1133 sb.append(' '); 957 1134 958 sb.append("</" + e.getNodeName() + ">\n");959 }960 961 }962 963 public static String xmlNodeToStringWithoutIndenting(Node e)964 {965 StringBuffer sb = new StringBuffer("");966 xmlNodeToStringWithoutNewline(sb, e, -1);967 return sb.toString();968 }969 970 public static String xmlNodeToStringWithoutNewline(Node e)971 {972 StringBuffer sb = new StringBuffer("");973 xmlNodeToStringWithoutNewline(sb, e, 0);974 return sb.toString();975 }976 977 private static void xmlNodeToStringWithoutNewline(StringBuffer sb, Node e, int depth)978 {979 980 for (int i = 0; i < depth; i++)981 {982 sb.append(' ');983 }984 985 if (e.getNodeType() == Node.TEXT_NODE)986 {987 if (e.getNodeValue() != "")988 {989 sb.append(e.getNodeValue().replaceAll("&", "&").replaceAll("<", "<").replace(">", ">"));990 }991 return;992 }993 994 if (e.getNodeType() == Node.COMMENT_NODE)995 {996 if (e.getNodeValue() != "")997 {998 sb.append("<!--" + e.getNodeValue() + "-->");999 }1000 return;1001 }1002 1003 sb.append('<');1004 sb.append(e.getNodeName());1005 NamedNodeMap attrs = e.getAttributes();1006 if (attrs != null)1007 {1008 for (int i = 0; i < attrs.getLength(); i++)1009 {1010 Node attr = attrs.item(i);1011 sb.append(' ');1012 sb.append(attr.getNodeName());1013 sb.append("=\"");1014 sb.append(attr.getNodeValue());1015 sb.append('"');1016 }1017 }1018 NodeList children = e.getChildNodes();1019 1020 if (children == null || children.getLength() == 0)1021 sb.append("/>");1022 else1023 {1024 1025 sb.append(">");1026 1027 int len = children.getLength();1028 for (int i = 0; i < len; i++)1029 {1030 if (depth >= 0)1031 {1032 xmlNodeToStringWithoutNewline(sb, children.item(i), depth + 1);1033 }1034 else1035 {1036 xmlNodeToStringWithoutNewline(sb, children.item(i), depth);1037 }1038 }1039 1040 for (int i = 0; i < depth; i++)1041 sb.append(' ');1042 1043 1135 sb.append("</" + e.getNodeName() + ">"); 1044 1136 }
Note:
See TracChangeset
for help on using the changeset viewer.