001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.pdp.xml.impl;
017:
018: import java.io.FileInputStream;
019: import java.io.FileNotFoundException;
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import javax.xml.parsers.ParserConfigurationException;
024: import javax.xml.parsers.SAXParser;
025: import javax.xml.parsers.SAXParserFactory;
026:
027: import org.kuali.core.service.KualiConfigurationService;
028: import org.kuali.module.pdp.exception.ConfigurationError;
029: import org.kuali.module.pdp.exception.FileReadException;
030: import org.kuali.module.pdp.xml.PaymentFileParser;
031: import org.kuali.module.pdp.xml.PdpFileHandler;
032: import org.kuali.module.pdp.xml.XmlAccounting;
033: import org.kuali.module.pdp.xml.XmlDetail;
034: import org.kuali.module.pdp.xml.XmlGroup;
035: import org.kuali.module.pdp.xml.XmlHeader;
036: import org.kuali.module.pdp.xml.XmlTrailer;
037: import org.xml.sax.Attributes;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.SAXNotRecognizedException;
040: import org.xml.sax.SAXNotSupportedException;
041: import org.xml.sax.SAXParseException;
042: import org.xml.sax.helpers.DefaultHandler;
043:
044: public class PaymentFileParserImpl extends DefaultHandler implements
045: PaymentFileParser {
046: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
047: .getLogger(PaymentFileParserImpl.class);
048:
049: static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
050: static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
051: static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
052:
053: private PdpFileHandler fileHandler = null;
054: private KualiConfigurationService kualiConfigurationService;
055:
056: public PaymentFileParserImpl() {
057: }
058:
059: public void setFileHandler(PdpFileHandler fileHandler) {
060: this .fileHandler = fileHandler;
061: }
062:
063: public void setKualiConfigurationService(KualiConfigurationService p) {
064: kualiConfigurationService = p;
065: }
066:
067: public void parse(InputStream stream) throws FileReadException {
068: String xsdUrl = kualiConfigurationService
069: .getPropertyString("externalizable.xml.url")
070: + "fs/payment1.xsd";
071: LOG.info("parse() xsdUrl: " + xsdUrl);
072:
073: if (fileHandler == null) {
074: throw new ConfigurationError("fileHandler not set");
075: }
076:
077: SAXParserFactory factory = SAXParserFactory.newInstance();
078: factory.setNamespaceAware(true);
079: factory.setValidating(true);
080:
081: try {
082: // Parse the input
083: SAXParser saxParser = factory.newSAXParser();
084: try {
085: saxParser.setProperty(JAXP_SCHEMA_LANGUAGE,
086: W3C_XML_SCHEMA);
087: } catch (SAXNotRecognizedException x) {
088: LOG
089: .error(
090: "parse() JAXP version can't validate with schemas",
091: x);
092: throw new ConfigurationError(
093: "JAXP Parser doesn't support schemas");
094: }
095: LOG.debug("parse() before setProperty");
096: saxParser.setProperty(JAXP_SCHEMA_SOURCE, xsdUrl);
097: LOG.debug("parse() before SAX parse");
098: saxParser.parse(stream, this );
099: LOG.debug("parse() after SAX parse");
100: } catch (IllegalArgumentException e) {
101: LOG.error("parse() stream is null", e);
102: throw new FileReadException("Unable to read input stream: "
103: + e.getMessage());
104: } catch (SAXNotSupportedException e) {
105: LOG.error("parse() SAX not supported in the parser", e);
106: throw new ConfigurationError("No JAXP SAX Parser");
107: } catch (ParserConfigurationException e) {
108: LOG.error("parse() Parser configuration error", e);
109: throw new ConfigurationError("Parser configuration error: "
110: + e.getMessage());
111: } catch (SAXException e) {
112: LOG.error("parse() Unknown SAX exception", e);
113: throw new FileReadException("SAX parser error "
114: + e.getMessage());
115: } catch (IOException e) {
116: LOG.error("parse() Unable to read file", e);
117: throw new FileReadException(
118: "IO error, unable to read input stream: "
119: + e.getMessage());
120: }
121: }
122:
123: public void parse(String filename) throws FileReadException {
124: try {
125: parse(new FileInputStream(filename));
126: } catch (FileNotFoundException e) {
127: LOG.error("parse() File not found", e);
128: throw new FileReadException("File not found");
129: }
130: }
131:
132: private TagStack tags = new TagStack();
133: private StringBuffer chars = null;
134:
135: private XmlHeader header = null;
136: private XmlGroup group = null;
137: private XmlDetail detail = null;
138: private XmlAccounting accounting = null;
139: private XmlTrailer trailer = null;
140:
141: private String tagPath;
142:
143: public void startElement(String namespaceURI, String lName,
144: String qName, Attributes attrs) throws SAXException {
145: tags.push(qName);
146: chars = new StringBuffer();
147:
148: if ("pdp_file/group/payee_id".equals(tags.getTagPath())) {
149: group.setId_type(attrs.getValue(0));
150: } else if ("pdp_file/header".equals(tags.getTagPath())) {
151: header = new XmlHeader();
152: } else if ("pdp_file/trailer".equals(tags.getTagPath())) {
153: trailer = new XmlTrailer();
154: } else if ("pdp_file/group".equals(tags.getTagPath())) {
155: group = new XmlGroup();
156: } else if ("pdp_file/group/detail".equals(tags.getTagPath())) {
157: detail = new XmlDetail();
158: } else if ("pdp_file/group/detail/accounting".equals(tags
159: .getTagPath())) {
160: accounting = new XmlAccounting();
161: }
162: }
163:
164: public void endElement(String namespaceURI, String sName,
165: String qName) throws SAXException {
166: tagPath = tags.getTagPath();
167:
168: // clean chars of funny characters not to be loaded in PDP
169: StringBuffer data = chars;
170: for (int i = (data.length() - 1); i >= 0; i--) {
171: char element = data.charAt(i);
172: if (element < 32) {
173: chars.deleteCharAt(i);
174: }
175: }
176:
177: if ("pdp_file/header".equals(tagPath)) {
178: fileHandler.setHeader(header);
179: header = null;
180: } else if ("pdp_file/trailer".equals(tagPath)) {
181: fileHandler.setTrailer(trailer);
182: trailer = null;
183: } else if ("pdp_file/group".equals(tagPath)) {
184: fileHandler.setGroup(group);
185: group = null;
186: } else if ("pdp_file/group/detail".equals(tagPath)) {
187: group.addDetail(detail);
188: detail = null;
189: } else if ("pdp_file/group/detail/accounting".equals(tagPath)) {
190: detail.addAccounting(accounting);
191: accounting = null;
192: } else if ("payment_text".equals(qName)) {
193: if (detail != null) {
194: detail.addPayment_text(chars.toString());
195: }
196: } else if (tagPath
197: .startsWith("pdp_file/group/detail/accounting/")) {
198: if (accounting != null) {
199: accounting.setField(qName, chars.toString());
200: }
201: } else if (tagPath.startsWith("pdp_file/group/detail/")) {
202: if (detail != null) {
203: detail.setField(qName, chars.toString());
204: }
205: } else if (tagPath.startsWith("pdp_file/group/")) {
206: if (group != null) {
207: group.setField(qName, chars.toString());
208: }
209: } else if (tagPath.startsWith("pdp_file/header/")) {
210: if (header != null) {
211: header.setField(qName, chars.toString());
212: }
213: } else if (tagPath.startsWith("pdp_file/trailer/")) {
214: if (trailer != null) {
215: trailer.setField(qName, chars.toString());
216: }
217: } else if ("payment_text".equals(qName)) {
218: if (detail != null) {
219: detail.addPayment_text(chars.toString());
220: }
221: }
222: tags.pop();
223: }
224:
225: public void characters(char buf[], int offset, int len)
226: throws SAXException {
227: chars.append(buf, offset, len);
228: }
229:
230: public void error(SAXParseException pe) throws SAXException {
231: LOG.error("error() Exception", pe);
232: fileHandler.setErrorMessage(pe.getMessage());
233: }
234:
235: public void fatalError(SAXParseException pe) throws SAXException {
236: LOG.error("fatalError() Exception", pe);
237: fileHandler.setErrorMessage(pe.getMessage());
238: }
239:
240: public void warning(SAXParseException pe) throws SAXException {
241: LOG.error("warning() Exception", pe);
242: fileHandler.setErrorMessage(pe.getMessage());
243: }
244: }
|