001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.data.ows;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import org.geotools.ows.ServiceException;
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.JDOMException;
027: import org.jdom.input.SAXBuilder;
028:
029: /**
030: * Utility class that will parse ServiceExceptions out of an inputStream.
031: *
032: * @author rgould
033: *
034: */
035: public class ServiceExceptionParser {
036:
037: /**
038: * Tries to read a ServiceExceptionReport from the input stream, and
039: * construct a chain of ServiceExceptions.
040: *
041: * ServiceExceptions beyond the first can be accessed using
042: * ServiceException.next();
043: *
044: * @param inputStream
045: * @throws JDOMException
046: * @throws IOException
047: */
048: public static ServiceException parse(InputStream inputStream)
049: throws JDOMException, IOException {
050: SAXBuilder builder = new SAXBuilder();
051: Document document = builder.build(inputStream);
052:
053: Element root = document.getRootElement();
054: List serviceExceptions = root.getChildren("ServiceException");
055:
056: /*
057: * ServiceExceptions with codes get bumped to the top of the list.
058: */
059: List codes = new ArrayList();
060: List noCodes = new ArrayList();
061: for (int i = 0; i < serviceExceptions.size(); i++) {
062: Element element = (Element) serviceExceptions.get(i);
063: ServiceException exception = parseSE(element);
064: if (exception.getCode() != null
065: && exception.getCode().length() != 0) {
066: codes.add(exception);
067: } else {
068: noCodes.add(exception);
069: }
070: }
071:
072: /*
073: * Now chain them.
074: */
075: ServiceException firstException = null;
076: ServiceException recentException = null;
077: for (int i = 0; i < codes.size(); i++) {
078: ServiceException exception = (ServiceException) codes
079: .get(i);
080: if (firstException == null) {
081: firstException = exception;
082: recentException = exception;
083: } else {
084: recentException.setNext(exception);
085: recentException = exception;
086: }
087: }
088: codes = null;
089: for (int i = 0; i < noCodes.size(); i++) {
090: ServiceException exception = (ServiceException) noCodes
091: .get(i);
092: if (firstException == null) {
093: firstException = exception;
094: recentException = exception;
095: } else {
096: recentException.setNext(exception);
097: recentException = exception;
098: }
099: }
100: noCodes = null;
101:
102: return firstException;
103: }
104:
105: private static ServiceException parseSE(Element element) {
106: String errorMessage = element.getText();
107: String code = element.getAttributeValue("code");
108:
109: return new ServiceException(errorMessage, code);
110: }
111:
112: }
|