1 package com.bull.eclipse.jonas; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 7 13 14 public class ExtraResourcesEntries { 15 public static final String TAG_NAME = "extraResourcesEntries"; 16 private static final String ENTRY_TAG_NAME = "extraResourcesEntry"; 17 private List entries; 18 19 20 public ExtraResourcesEntries() { 21 entries = new ArrayList (); 22 } 23 24 public ExtraResourcesEntries(String [] values, int size) { 25 int i = 0; 26 entries = new ArrayList (); 27 while (i < size) { 28 entries.add(values[i]); 29 i++; 30 } 31 } 32 33 34 public int size() { 35 return entries.size(); 36 } 37 38 39 public String getExtraResourcesEntry(int index) { 40 if (index >= this.size()) return null; 41 String entry = (String ) entries.get(index); 42 return entry; 43 } 44 45 46 public void addExtraResourcesEntry(String value) { 47 if (entries.contains(value)) return; 48 entries.add(value); 49 } 50 51 public List getList() { return entries; } 52 53 56 public String xmlMarshal() { 57 return xmlMarshal(0); 58 } 59 60 public String xmlMarshal(int spacesToIntend) { 61 String spaces = ""; 62 for(int i=0; i < spacesToIntend; i++) { 63 spaces = spaces+" "; 64 } 65 String xml = spaces + startTag() + "\n"; 66 67 for (Iterator it = entries.iterator(); it.hasNext();) { 68 String entry = (String ) it.next(); 69 xml += spaces + spaces + startEntryTag() + entry + endEntryTag() + "\n"; 70 } 71 72 xml += spaces + endTag() + "\n"; 73 return xml; 74 } 75 76 82 83 public static ExtraResourcesEntries xmlUnmarshal(String xmlString) { 84 if (xmlString == null || xmlString.trim().length() == 0) { 85 return null; 86 } 87 int start = xmlString.indexOf(startTag()); 88 int end = xmlString.indexOf(endTag()); 89 if (start < 0 || end <= start) return null; 90 String value = xmlString.substring(start+startTag().length(), end); 91 value = value.trim(); 92 93 ExtraResourcesEntries extraEntries = new ExtraResourcesEntries(); 94 while(value != null && value.length() > 0) { 95 start = value.indexOf(startEntryTag()); 96 end = value.indexOf(endEntryTag()); 97 if (start >= 0 || end > start) { 98 String entryValue = value.substring(start+startEntryTag().length(), end); 99 if (entryValue.trim().length() > 0) { 100 extraEntries.addExtraResourcesEntry(entryValue); 101 } 102 value = value.substring(end + endEntryTag().length()); 103 } else { 104 value = null; 105 } 106 } 107 108 return extraEntries; 109 } 110 111 private static String startTag() { return "<" + TAG_NAME + ">"; } 112 private static String endTag() { return "</" + TAG_NAME + ">"; } 113 private static String startEntryTag() { return "<" + ENTRY_TAG_NAME + ">"; } 114 private static String endEntryTag() { return "</" + ENTRY_TAG_NAME + ">"; } 115 116 117 121 public static void main(String [] arguments) { 122 String xml = ""; 123 ExtraResourcesEntries entries = xmlUnmarshal(xml); 124 125 if (entries != null) { 126 System.err.println("invalid xml must result in null object !"); 127 System.exit(1); 128 } 129 130 xml = "<extraResourcesEntries></extraResourcesEntries>"; 131 entries = xmlUnmarshal(xml); 132 if (entries == null) { 133 System.err.println("valid xml must result in an object !"); 134 System.exit(1); 135 } 136 if (entries.size() != 0) { 137 System.err.println("expected size 0 but was " + entries.size()); 138 System.exit(1); 139 } 140 xml = "<root><extraResourcesEntries>\n</extraResourcesEntries>\n</root>"; 141 entries = xmlUnmarshal(xml); 142 if (entries == null) { 143 System.err.println("valid xml must result in an object !"); 144 System.exit(1); 145 } 146 if (entries.size() != 0) { 147 System.err.println("expected size 0 but was " + entries.size()); 148 System.exit(1); 149 } 150 151 xml = "<extraResourcesEntries><extraResourcesEntry>abc</extraResourcesEntry></extraResourcesEntries>"; 152 entries = xmlUnmarshal(xml); 153 if (entries == null) { 154 System.err.println("valid xml must result in an object !"); 155 System.exit(1); 156 } 157 if (entries.size() != 1) { 158 System.err.println("expected size 1 but was " + entries.size()); 159 System.exit(1); 160 } 161 if (!entries.getExtraResourcesEntry(0).equals("abc")) { 162 System.err.println("expected 'abc' but was '" + entries.getExtraResourcesEntry(0) + "'"); 163 System.exit(1); 164 } 165 166 xml = "<extraResourcesEntries>\n<extraResourcesEntry>abc</extraResourcesEntry>\n<extraResourcesEntry>def</extraResourcesEntry>\n<extraResourcesEntry>123</extraResourcesEntry>\nxxxxx</extraResourcesEntries>\n"; 167 entries = xmlUnmarshal(xml); 168 if (entries == null) { 169 System.err.println("valid xml must result in an object !"); 170 System.exit(1); 171 } 172 if (entries.size() != 3) { 173 System.err.println("expected size 1 but was " + entries.size()); 174 System.exit(1); 175 } 176 if (!entries.getExtraResourcesEntry(0).equals("abc")) { 177 System.err.println("expected 'abc' but was '" + entries.getExtraResourcesEntry(0) + "'"); 178 System.exit(1); 179 } 180 if (!entries.getExtraResourcesEntry(1).equals("def")) { 181 System.err.println("expected 'def' but was '" + entries.getExtraResourcesEntry(1) + "'"); 182 System.exit(1); 183 } 184 if (!entries.getExtraResourcesEntry(2).equals("123")) { 185 System.err.println("expected '123' but was '" + entries.getExtraResourcesEntry(2) + "'"); 186 System.exit(1); 187 } 188 189 xml = "<extraResourcesEntries>\n<extraResourcesEntry>abc</extraResourcesEntry>\n<extraResourcesEntry>def</extraResourcesEntry>\n<extraResourcesEntry>123</extraResourcesEntry>\n</extraResourcesEntries>\n"; 190 String gen = entries.xmlMarshal(); 191 if (gen.equals(xml) == false) { 192 System.err.println("generated xml is incorrect:\n!" + gen + "!"); 193 System.err.println("expected xml is :\n!" + xml + "!"); 194 System.exit(1); 195 } 196 197 System.out.println("All okay !"); 198 } 199 } 200 | Popular Tags |