1 16 17 package xni.parser; 18 19 import java.io.BufferedReader ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.Reader ; 24 import java.util.StringTokenizer ; 25 26 import org.apache.xerces.util.NamespaceSupport; 27 import org.apache.xerces.util.XMLAttributesImpl; 28 import org.apache.xerces.util.XMLStringBuffer; 29 import org.apache.xerces.xni.QName; 30 import org.apache.xerces.xni.XMLAttributes; 31 import org.apache.xerces.xni.XMLDTDContentModelHandler; 32 import org.apache.xerces.xni.XMLString; 33 import org.apache.xerces.xni.XNIException; 34 import org.apache.xerces.xni.parser.XMLInputSource; 35 36 65 public class CSVConfiguration 66 extends AbstractConfiguration { 67 68 72 73 protected static final QName CSV = new QName(null, null, "csv", null); 74 75 76 protected static final QName ROW = new QName(null, null, "row", null); 77 78 79 protected static final QName COL = new QName(null, null, "col", null); 80 81 82 protected static final XMLAttributes EMPTY_ATTRS = new XMLAttributesImpl(); 83 84 85 private final XMLString NEWLINE = new XMLStringBuffer("\n"); 86 87 88 private final XMLString NEWLINE_ONE_SPACE = new XMLStringBuffer("\n "); 89 90 91 private final XMLString NEWLINE_TWO_SPACES = new XMLStringBuffer("\n "); 92 93 97 101 private final XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 102 103 107 131 public void parse(XMLInputSource source) 132 throws IOException , XNIException { 133 134 openInputSourceStream(source); 136 Reader reader = source.getCharacterStream(); 137 if (reader == null) { 138 InputStream stream = source.getByteStream(); 139 reader = new InputStreamReader (stream); 140 } 141 BufferedReader bufferedReader = new BufferedReader (reader); 142 143 if (fDocumentHandler != null) { 145 fDocumentHandler.startDocument(null, "UTF-8", new NamespaceSupport(), null); 146 fDocumentHandler.xmlDecl("1.0", "UTF-8", "true", null); 147 fDocumentHandler.doctypeDecl("csv", null, null, null); 148 } 149 if (fDTDHandler != null) { 150 fDTDHandler.startDTD(null, null); 151 fDTDHandler.elementDecl("csv", "(row)*", null); 152 fDTDHandler.elementDecl("row", "(col)*", null); 153 fDTDHandler.elementDecl("col", "(#PCDATA)", null); 154 } 155 if (fDTDContentModelHandler != null) { 156 fDTDContentModelHandler.startContentModel("csv", null); 157 fDTDContentModelHandler.startGroup(null); 158 fDTDContentModelHandler.element("row", null); 159 fDTDContentModelHandler.endGroup(null); 160 short csvOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE; 161 fDTDContentModelHandler.occurrence(csvOccurs, null); 162 fDTDContentModelHandler.endContentModel(null); 163 164 fDTDContentModelHandler.startContentModel("row", null); 165 fDTDContentModelHandler.startGroup(null); 166 fDTDContentModelHandler.element("col", null); 167 fDTDContentModelHandler.endGroup(null); 168 short rowOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE; 169 fDTDContentModelHandler.occurrence(rowOccurs, null); 170 fDTDContentModelHandler.endContentModel(null); 171 172 fDTDContentModelHandler.startContentModel("col", null); 173 fDTDContentModelHandler.startGroup(null); 174 fDTDContentModelHandler.pcdata(null); 175 fDTDContentModelHandler.endGroup(null); 176 fDTDContentModelHandler.endContentModel(null); 177 } 178 if (fDTDHandler != null) { 179 fDTDHandler.endDTD(null); 180 } 181 if (fDocumentHandler != null) { 182 fDocumentHandler.startElement(CSV, EMPTY_ATTRS, null); 183 } 184 185 String line; 187 while ((line = bufferedReader.readLine()) != null) { 188 if (fDocumentHandler != null) { 189 fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null); 190 fDocumentHandler.startElement(ROW, EMPTY_ATTRS, null); 191 StringTokenizer tokenizer = new StringTokenizer (line, ","); 192 while (tokenizer.hasMoreTokens()) { 193 fDocumentHandler.ignorableWhitespace(NEWLINE_TWO_SPACES, null); 194 fDocumentHandler.startElement(COL, EMPTY_ATTRS, null); 195 String token = tokenizer.nextToken(); 196 fStringBuffer.clear(); 197 fStringBuffer.append(token); 198 fDocumentHandler.characters(fStringBuffer, null); 199 fDocumentHandler.endElement(COL, null); 200 } 201 fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null); 202 fDocumentHandler.endElement(ROW, null); 203 } 204 } 205 bufferedReader.close(); 206 207 if (fDocumentHandler != null) { 209 fDocumentHandler.ignorableWhitespace(NEWLINE, null); 210 fDocumentHandler.endElement(CSV, null); 211 fDocumentHandler.endDocument(null); 212 } 213 214 } 216 221 public void setFeature(String featureId, boolean state) {} 222 public boolean getFeature(String featureId) { return false; } 223 public void setProperty(String propertyId, Object value) {} 224 public Object getProperty(String propertyId) { return null; } 225 226 } | Popular Tags |