1 26 27 package org.nightlabs.editor2d.iofilter; 28 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.util.ArrayList ; 33 import java.util.List ; 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 reader; 46 47 protected boolean debug = false; 48 49 public ManifestReader(InputStream in) 50 { 51 super(); 52 try { 53 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 54 factory.setNamespaceAware(true); 55 xpp = factory.newPullParser(); 56 reader = new InputStreamReader (in); 57 xpp.setInput(reader); 58 } catch (XmlPullParserException e) { 59 throw new RuntimeException (e); 60 } 61 } 62 63 protected static final String APPLICATION = "application/"; 64 protected static final String TEXT = "text/xml"; 65 protected static final String 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 attrName = xpp.getAttributeName(i); 93 String 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 } catch (XmlPullParserException e) { 110 throw new RuntimeException (e); 111 } catch (IOException e) { 112 throw new RuntimeException (e); 113 } 114 } 115 116 public void close() 117 { 118 try { 119 reader.close(); 120 } catch (IOException e) { 121 throw new RuntimeException (e); 122 } 123 } 124 125 protected String nameSpace; 126 131 public String getNameSpace() { 132 return nameSpace; 133 } 134 135 protected String contentType; 136 140 public String getContentType() { 141 return contentType; 142 } 143 144 protected List imageInfo; 145 151 public List getImageInfo() 152 { 153 if (imageInfo == null) 154 imageInfo = new ArrayList (); 155 156 return imageInfo; 157 } 158 159 protected boolean foundContentType = false; 160 protected void findContentType(String s) 161 { 162 StringBuffer sb = new StringBuffer (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 attrName, int attributeIndex) 173 { 174 StringBuffer sb = new StringBuffer (attrName); 175 if (sb.indexOf(IMAGE) != -1) 176 { 177 String [] imageInfo = new String [2]; 178 179 String imageType = removeDirectory(attrName); 180 String imagePath = xpp.getAttributeValue(attributeIndex + 1); 181 String 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 removeDirectory(String s) 195 { 196 StringBuffer sb = new StringBuffer (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 |