KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > ExtraResourcesEntries


1 package com.bull.eclipse.jonas;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 /**
8  * container for managing a number of ExtraResourcesEntry objects
9  *
10  * @version 1.0
11  * @author Emmanuel Rias
12  */

13
14 public class ExtraResourcesEntries {
15     public static final String JavaDoc TAG_NAME = "extraResourcesEntries";
16     private static final String JavaDoc ENTRY_TAG_NAME = "extraResourcesEntry";
17     private List JavaDoc entries;
18     
19
20     public ExtraResourcesEntries() {
21             entries = new ArrayList JavaDoc();
22     }
23     
24     public ExtraResourcesEntries(String JavaDoc[] values, int size) {
25         int i = 0;
26         entries = new ArrayList JavaDoc();
27         while (i < size) {
28             entries.add(values[i]);
29             i++;
30         }
31     }
32     
33     /** returns the number of extra resources-entries */
34     public int size() {
35         return entries.size();
36     }
37     
38     /** return the Extra ResourcesEntry value at the index provided */
39     public String JavaDoc getExtraResourcesEntry(int index) {
40         if (index >= this.size()) return null;
41         String JavaDoc entry = (String JavaDoc) entries.get(index);
42         return entry;
43     }
44     
45     /** add a Extra Resources Entry value */
46     public void addExtraResourcesEntry(String JavaDoc value) {
47         if (entries.contains(value)) return;
48         entries.add(value);
49     }
50     
51     public List JavaDoc getList() { return entries; }
52
53     /**
54      * transfer the state of this object to an XML string
55      */

56     public String JavaDoc xmlMarshal() {
57         return xmlMarshal(0);
58     }
59
60     public String JavaDoc xmlMarshal(int spacesToIntend) {
61         String JavaDoc spaces = "";
62         for(int i=0; i < spacesToIntend; i++) {
63             spaces = spaces+" ";
64         }
65         String JavaDoc xml = spaces + startTag() + "\n";
66         
67         for (Iterator JavaDoc it = entries.iterator(); it.hasNext();) {
68             String JavaDoc entry = (String JavaDoc) it.next();
69             xml += spaces + spaces + startEntryTag() + entry + endEntryTag() + "\n";
70         }
71                     
72         xml += spaces + endTag() + "\n";
73         return xml;
74     }
75     
76     /**
77      * instantiate a Extra Resources Entries object and intialize
78      * it with the xml data provided
79      * @return the object if unmarshaling had no errors. returns null
80      * if the marshaling was unsuccessfully.
81      */

82     
83     public static ExtraResourcesEntries xmlUnmarshal(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc startTag() { return "<" + TAG_NAME + ">"; }
112     private static String JavaDoc endTag() { return "</" + TAG_NAME + ">"; }
113     private static String JavaDoc startEntryTag() { return "<" + ENTRY_TAG_NAME + ">"; }
114     private static String JavaDoc endEntryTag() { return "</" + ENTRY_TAG_NAME + ">"; }
115     
116     
117     /**
118      * main method yust for some simple tests - should be in a Junit Testclass
119      * but I don't want to add the junit reference to this project
120      */

121     public static void main(String JavaDoc[] arguments) {
122         String JavaDoc 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 JavaDoc 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