Source code: org/integralsource/monsoon/io/ListReader.java
1 /*
2 * Copyright (c) 2001 John Keyes
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to:
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * $Id: ListReader.java,v 1.3 2001/05/28 23:08:25 jbjk Exp $
20 */
21 package org.integralsource.monsoon.io;
22
23 public class ListReader {
24
25 public ListReader(java.lang.String aList) {
26
27 javax.xml.parsers.DocumentBuilderFactory
28 $factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
29
30 javax.xml.parsers.DocumentBuilder $builder = null;
31
32 try {
33 $builder = $factory.newDocumentBuilder();
34
35 }
36 catch(javax.xml.parsers.ParserConfigurationException $parse) {
37 $parse.printStackTrace(System.err);
38 }
39
40 try {
41 _list = $builder.parse("data/"+aList+".xml");
42 }
43 // TODO [jkeyes 28 May 2000] add error handling
44 catch(org.xml.sax.SAXException $saxExp) {
45 $saxExp.printStackTrace(System.err);
46 }
47 catch(java.io.IOException $ioExp) {
48 $ioExp.printStackTrace(System.err);
49 }
50 }
51
52 public org.w3c.dom.Document getDOM() {
53 return _list;
54 }
55
56 public java.util.ArrayList getItems() {
57 java.util.ArrayList $items = new java.util.ArrayList();
58
59 org.w3c.dom.Node $listNode = _list.getDocumentElement();
60
61 org.w3c.dom.NodeList $children = $listNode.getChildNodes();
62
63 int $nodes = $children.getLength();
64
65 for(int i=0; i < $nodes; i++) {
66 org.integralsource.monsoon.data.Item $item
67 = getItem($children.item(i));
68
69 if($item != null) {
70 $items.add($item);
71 }
72 }
73
74 return $items;
75 }
76
77 private org.integralsource.monsoon.data.Item
78 getItem(org.w3c.dom.Node node) {
79
80 if(node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
81 && "item".equals(node.getNodeName())) {
82 org.w3c.dom.Element $element = (org.w3c.dom.Element)node;
83
84 java.lang.String $id = $element.getAttribute("id");
85 java.lang.String $description = null;
86 java.lang.String $title = null;
87 java.lang.String $priority = null;
88 java.lang.String $complete = null;
89
90 org.w3c.dom.NodeList $children = node.getChildNodes();
91
92 int $nodes = $children.getLength();
93
94 for(int i = 0; i < $nodes; i++) {
95 if($title == null) {
96 $title = getItemTitle($children.item(i));
97 }
98 if($description == null) {
99 $description = getItemDescription($children.item(i));
100 }
101 if($priority == null) {
102 $priority = getItemPriority($children.item(i));
103 }
104 if($complete == null) {
105 $complete = isItemComplete($children.item(i));
106 }
107 }
108 return
109 new org.integralsource.monsoon.data.Item($id,
110 $title,
111 $description,
112 $priority,
113 new java.lang.Boolean($complete));
114 }
115 return null;
116 }
117
118 private java.lang.String getItemTitle(org.w3c.dom.Node node) {
119 return getText("title",node);
120 }
121
122 private java.lang.String getItemDescription(org.w3c.dom.Node node) {
123 return getText("description",node);
124 }
125
126 private java.lang.String getItemPriority(org.w3c.dom.Node node) {
127 return getText("priority",node);
128 }
129
130 private java.lang.String isItemComplete(org.w3c.dom.Node node) {
131 return getText("complete",node);
132 }
133
134 private java.lang.String getText(java.lang.String nodeName,
135 org.w3c.dom.Node node) {
136
137 if(node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE
138 && nodeName.equals(node.getNodeName())) {
139 org.w3c.dom.NodeList $children = node.getChildNodes();
140
141 int $nodes = $children.getLength();
142
143 java.lang.String $text = null;
144
145 for(int i=0 ; i < $nodes; i++) {
146 org.w3c.dom.Node $n = $children.item(i);
147 if($n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
148 $text = $n.getNodeValue();
149 }
150 }
151 return $text.trim();
152 }
153 return null;
154 }
155
156 // member variable
157 private org.w3c.dom.Document _list;
158
159 }