KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BestDeal


1 import org.xmlpull.v1.*;
2 import java.io.*;
3 import java.util.*;
4 import java.text.MessageFormat JavaDoc;
5
6 /**
7  * Rewritten http://www-106.ibm.com/developerworks/xml/library/x-saxapi/listing4.html
8  * to make easy to comapre how to "natutal is to manage state with SAX and with XMLPULL API.
9  * For details see: http://www-106.ibm.com/developerworks/xml/library/x-saxapi/
10  */

11 public class BestDeal {
12
13     protected static final String JavaDoc MESSAGE =
14         "The best deal is proposed by {0}. "
15         + "A(n) {1} delivered in {2,number,integer} days for "
16         + "{3,number,currency}";
17
18     protected static final String JavaDoc NAMESPACE_URI =
19         "http://www.psol.com/xbe2/listing8.3";
20
21     /**
22      * properties we are collecting: best price, delivery time,
23      * product and vendor names
24      */

25     public double price = Double.MAX_VALUE;
26     public int delivery = Integer.MAX_VALUE;
27     public String JavaDoc product = null;
28     public String JavaDoc vendor = null;
29
30     /**
31      * target delivery value (refuse elements above this target)
32      */

33     protected int targetDelivery;
34
35     /**
36      * creates an "empty" BestDeal with the given target for delivery
37      * @param td the target for delivery
38      */

39     public BestDeal(int td) {
40         targetDelivery = td;
41     }
42
43     /**
44      * updates the best deal from the given list in the format
45      */

46     public void update(XmlPullParser parser)
47         throws IOException, XmlPullParserException {
48
49         parser.require(parser.START_DOCUMENT, null, null);
50
51         parser.nextTag();
52         parser.require(parser.START_TAG, NAMESPACE_URI, "price-list");
53
54         parser.nextTag();
55         parser.require(parser.START_TAG, NAMESPACE_URI, "name");
56         product = parser.nextText();
57         parser.require(parser.END_TAG, NAMESPACE_URI, "name");
58
59
60         while (parser.nextTag() == parser.START_TAG) {
61             checkVendor(parser);
62         }
63
64         parser.require(parser.END_TAG, NAMESPACE_URI, "price-list");
65
66         parser.next();
67         parser.require(parser.END_DOCUMENT, null, null);
68     }
69
70     /** subroutine handling a single vendor */
71     public void checkVendor(XmlPullParser parser)
72         throws IOException, XmlPullParserException {
73         parser.require(parser.START_TAG, NAMESPACE_URI, "vendor");
74         String JavaDoc currentVendor = null;
75
76         while (parser.nextTag() == parser.START_TAG) {
77             parser.require(parser.START_TAG, NAMESPACE_URI, null);
78             String JavaDoc name = parser.getName();
79             if (name.equals("name")) {
80                 currentVendor = parser.nextText();
81             } else if (name.equals("price-quote")) {
82                 int currentDelivery =
83                     Integer.parseInt(parser.getAttributeValue("", "delivery"));
84                 double currentPrice = Double.parseDouble(parser.nextText());
85
86                 if (currentDelivery < targetDelivery && currentPrice < price) {
87                     vendor = currentVendor;
88                     price = currentPrice;
89                     delivery = currentDelivery;
90                 }
91             } else {
92                 System.out.println("trying to skip unknwon element: "+ name);
93                 String JavaDoc content = parser.nextText();
94                 System.out.println("skipped element content:" + content);
95             }
96             parser.require(parser.END_TAG, NAMESPACE_URI, name);
97         }
98         parser.require(parser.END_TAG, NAMESPACE_URI, "vendor");
99     }
100
101     /**
102      * main() method
103      * decodes command-line parameters and invoke the parser
104      * @param args command-line argument
105      * @throw Exception catch-all for underlying exceptions
106      */

107     public static void main(String JavaDoc[] args)
108         throws IOException, XmlPullParserException {
109
110         if (args.length < 2) {
111             System.out.println("BestDeal <file> <delivery>");
112             return;
113         }
114
115         BestDeal bestDeal = new BestDeal(Integer.parseInt(args[1]));
116
117         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
118         factory.setNamespaceAware(true);
119         XmlPullParser parser = factory.newPullParser();
120
121         InputStream is = new FileInputStream(args[0]);
122         parser.setInput(is, null);
123         bestDeal.update(parser);
124         is.close();
125
126         Object JavaDoc[] objects =
127             new Object JavaDoc[] {
128             bestDeal.vendor,
129                 bestDeal.product,
130                 new Integer JavaDoc(bestDeal.delivery),
131                 new Double JavaDoc(bestDeal.price)};
132
133         System.out.println(MessageFormat.format(MESSAGE, objects));
134     }
135
136 }
137
138
Popular Tags