KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > iofilter > ManifestReader


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.iofilter;
28
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.InputStreamReader JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.xmlpull.v1.XmlPullParser;
37 import org.xmlpull.v1.XmlPullParserException;
38 import org.xmlpull.v1.XmlPullParserFactory;
39
40 public class ManifestReader
41 {
42     public static final Logger LOGGER = Logger.getLogger(ManifestReader.class.getName());
43     
44     protected XmlPullParser xpp;
45     protected InputStreamReader JavaDoc reader;
46     
47     protected boolean debug = false;
48     
49     public ManifestReader(InputStream JavaDoc in)
50     {
51         super();
52         try {
53             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
54             factory.setNamespaceAware(true);
55       xpp = factory.newPullParser();
56             reader = new InputStreamReader JavaDoc(in);
57             xpp.setInput(reader);
58         } catch (XmlPullParserException e) {
59             throw new RuntimeException JavaDoc(e);
60         }
61     }
62     
63     protected static final String JavaDoc APPLICATION = "application/";
64     protected static final String JavaDoc TEXT = "text/xml";
65     protected static final String JavaDoc IMAGE = "image/";
66     
67     public void read()
68     {
69         nameSpace = xpp.getNamespace();
70         if (debug)
71             LOGGER.debug("nameSpace = " + nameSpace);
72         
73         int attributeCount = -1;
74         int eventType;
75         try {
76             eventType = xpp.getEventType();
77             while (eventType != XmlPullParser.END_DOCUMENT)
78             {
79         if(eventType == XmlPullParser.START_DOCUMENT) {
80             if (debug)
81                 LOGGER.debug("Start Document!");
82           } else if(eventType == XmlPullParser.END_DOCUMENT) {
83             if (debug)
84                 LOGGER.debug("End Document!");
85           } else if(eventType == XmlPullParser.START_TAG) {
86             if (debug)
87                 LOGGER.debug("Start Tag "+xpp.getName());
88             
89             attributeCount = xpp.getAttributeCount();
90             for (int i=0; i<attributeCount; i++)
91             {
92                 String JavaDoc attrName = xpp.getAttributeName(i);
93                 String JavaDoc value = xpp.getAttributeValue(i);
94                 if (!foundContentType)
95                     findContentType(value);
96                 
97                 checkImageEntry(value, i);
98                 
99                 if (debug)
100                     LOGGER.debug(value+" = " + value);
101             }
102           } else if(eventType == XmlPullParser.END_TAG) {
103             if (debug)
104                 LOGGER.debug("End Tag "+xpp.getName());
105           }
106         eventType = xpp.next();
107             }
108 // reader.close();
109
} catch (XmlPullParserException e) {
110             throw new RuntimeException JavaDoc(e);
111         } catch (IOException JavaDoc e) {
112             throw new RuntimeException JavaDoc(e);
113         }
114     }
115     
116     public void close()
117     {
118         try {
119             reader.close();
120         } catch (IOException JavaDoc e) {
121             throw new RuntimeException JavaDoc(e);
122         }
123     }
124     
125     protected String JavaDoc nameSpace;
126     /**
127      *
128      * @return the NameSpace of the Manifest
129      * if no NameSpace is available it returns null
130      */

131     public String JavaDoc getNameSpace() {
132         return nameSpace;
133     }
134     
135     protected String JavaDoc contentType;
136     /**
137      *
138      * @return the contentType of the Manifest
139      */

140     public String JavaDoc getContentType() {
141         return contentType;
142     }
143     
144     protected List JavaDoc imageInfo;
145     /**
146      *
147      * @return a List which contains a String[] for each ImageEntry
148      * String[0] = imageType (e.g. jpg)
149      * String[1] = relative Image Path (e.g. images/1000000000000320000002582C99D0FF.jpg)
150      */

151     public List JavaDoc getImageInfo()
152     {
153         if (imageInfo == null)
154             imageInfo = new ArrayList JavaDoc();
155         
156         return imageInfo;
157     }
158         
159     protected boolean foundContentType = false;
160     protected void findContentType(String JavaDoc s)
161     {
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
163         int index = index = sb.indexOf(APPLICATION);;
164         if (index != -1) {
165             contentType = sb.substring(index);
166             foundContentType = true;
167             if (debug)
168                 LOGGER.debug("contentType = "+contentType);
169         }
170     }
171
172     protected void checkImageEntry(String JavaDoc attrName, int attributeIndex)
173     {
174         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(attrName);
175         if (sb.indexOf(IMAGE) != -1)
176         {
177             String JavaDoc[] imageInfo = new String JavaDoc[2];
178             
179             String JavaDoc imageType = removeDirectory(attrName);
180             String JavaDoc imagePath = xpp.getAttributeValue(attributeIndex + 1);
181             String JavaDoc imageName = removeDirectory(imagePath);
182             imageInfo[0] = imageType;
183             imageInfo[1] = imageName;
184             getImageInfo().add(imageInfo);
185             
186             if (debug) {
187                 LOGGER.debug("Image "+getImageInfo().size());
188                 LOGGER.debug("imageType = "+imageType);
189                 LOGGER.debug("imagePath = "+imagePath);
190             }
191         }
192     }
193     
194     protected String JavaDoc removeDirectory(String JavaDoc s)
195     {
196         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
197         int index = sb.lastIndexOf("/");
198         if (index != -1) {
199             return sb.substring(index + 1);
200         }
201         return null;
202     }
203 }
204
Popular Tags