2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: ResponseHandlerBase.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.3 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:28:23 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote.protocol.parser; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.util.LinkedList; |
2 |
26 Feb 07 |
jari |
15 |
|
2 |
26 Feb 07 |
jari |
16 |
import org.tigr.util.ConfMap; |
2 |
26 Feb 07 |
jari |
17 |
import org.xml.sax.Attributes; |
2 |
26 Feb 07 |
jari |
18 |
import org.xml.sax.SAXException; |
2 |
26 Feb 07 |
jari |
19 |
import org.xml.sax.SAXParseException; |
2 |
26 Feb 07 |
jari |
20 |
import org.xml.sax.helpers.DefaultHandler; |
2 |
26 Feb 07 |
jari |
21 |
|
2 |
26 Feb 07 |
jari |
22 |
class ResponseHandlerBase extends DefaultHandler { |
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
// elements stack |
2 |
26 Feb 07 |
jari |
25 |
private LinkedList m_elements = new LinkedList(); |
2 |
26 Feb 07 |
jari |
// XML elements ( path ) checker |
2 |
26 Feb 07 |
jari |
27 |
protected CheckPath m_path = new CheckPath( m_elements ); |
2 |
26 Feb 07 |
jari |
// Configuration info |
2 |
26 Feb 07 |
jari |
29 |
protected ConfMap m_config; |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>ResponseHandlerBase</code> with specified configuration. |
2 |
26 Feb 07 |
jari |
33 |
*/ |
2 |
26 Feb 07 |
jari |
34 |
public ResponseHandlerBase(ConfMap cfg) { |
2 |
26 Feb 07 |
jari |
35 |
m_config = cfg; |
2 |
26 Feb 07 |
jari |
36 |
} |
2 |
26 Feb 07 |
jari |
37 |
|
2 |
26 Feb 07 |
jari |
38 |
/** |
2 |
26 Feb 07 |
jari |
* An element is started. |
2 |
26 Feb 07 |
jari |
40 |
*/ |
2 |
26 Feb 07 |
jari |
41 |
public void startElement(String uri, String localName, |
2 |
26 Feb 07 |
jari |
42 |
String qName, Attributes attributes) throws SAXException { |
2 |
26 Feb 07 |
jari |
43 |
m_elements.addLast( qName ); |
2 |
26 Feb 07 |
jari |
44 |
} |
2 |
26 Feb 07 |
jari |
45 |
|
2 |
26 Feb 07 |
jari |
46 |
/** |
2 |
26 Feb 07 |
jari |
* An element is finished. |
2 |
26 Feb 07 |
jari |
48 |
*/ |
2 |
26 Feb 07 |
jari |
49 |
public void endElement(String uri, String localName, String qName) throws SAXException { |
2 |
26 Feb 07 |
jari |
50 |
m_elements.removeLast(); |
2 |
26 Feb 07 |
jari |
51 |
} |
2 |
26 Feb 07 |
jari |
52 |
|
2 |
26 Feb 07 |
jari |
// ErrorHandler interface |
2 |
26 Feb 07 |
jari |
54 |
public void warning(SAXParseException ex) throws SAXException { |
2 |
26 Feb 07 |
jari |
55 |
processError( new Exception("[Warning] "+ |
2 |
26 Feb 07 |
jari |
56 |
getLocationString(ex)+": "+ |
2 |
26 Feb 07 |
jari |
57 |
ex.getMessage()) ); |
2 |
26 Feb 07 |
jari |
58 |
} |
2 |
26 Feb 07 |
jari |
59 |
|
2 |
26 Feb 07 |
jari |
60 |
public void error(SAXParseException ex) throws SAXException { |
2 |
26 Feb 07 |
jari |
61 |
processError( new Exception("[Error] "+ |
2 |
26 Feb 07 |
jari |
62 |
getLocationString(ex)+": "+ |
2 |
26 Feb 07 |
jari |
63 |
ex.getMessage()) ); |
2 |
26 Feb 07 |
jari |
64 |
} |
2 |
26 Feb 07 |
jari |
65 |
|
2 |
26 Feb 07 |
jari |
66 |
public void fatalError(SAXParseException ex) throws SAXException { |
2 |
26 Feb 07 |
jari |
67 |
processError( new Exception("[FatalError] "+ |
2 |
26 Feb 07 |
jari |
68 |
getLocationString(ex)+": "+ |
2 |
26 Feb 07 |
jari |
69 |
ex.getMessage())); |
2 |
26 Feb 07 |
jari |
70 |
} |
2 |
26 Feb 07 |
jari |
71 |
|
2 |
26 Feb 07 |
jari |
72 |
/** |
2 |
26 Feb 07 |
jari |
* Returns location of an exception. |
2 |
26 Feb 07 |
jari |
74 |
*/ |
2 |
26 Feb 07 |
jari |
75 |
private String getLocationString(SAXParseException ex) { |
2 |
26 Feb 07 |
jari |
76 |
StringBuffer str = new StringBuffer(); |
2 |
26 Feb 07 |
jari |
77 |
|
2 |
26 Feb 07 |
jari |
78 |
String systemId = ex.getSystemId(); |
2 |
26 Feb 07 |
jari |
79 |
if (systemId != null) { |
2 |
26 Feb 07 |
jari |
80 |
int index = systemId.lastIndexOf('/'); |
2 |
26 Feb 07 |
jari |
81 |
if (index != -1) |
2 |
26 Feb 07 |
jari |
82 |
systemId = systemId.substring(index + 1); |
2 |
26 Feb 07 |
jari |
83 |
str.append(systemId); |
2 |
26 Feb 07 |
jari |
84 |
} |
2 |
26 Feb 07 |
jari |
85 |
str.append(':'); |
2 |
26 Feb 07 |
jari |
86 |
str.append(ex.getLineNumber()); |
2 |
26 Feb 07 |
jari |
87 |
str.append(':'); |
2 |
26 Feb 07 |
jari |
88 |
str.append(ex.getColumnNumber()); |
2 |
26 Feb 07 |
jari |
89 |
|
2 |
26 Feb 07 |
jari |
90 |
return str.toString(); |
2 |
26 Feb 07 |
jari |
91 |
} |
2 |
26 Feb 07 |
jari |
92 |
|
2 |
26 Feb 07 |
jari |
93 |
/** |
2 |
26 Feb 07 |
jari |
* Returns a current element. |
2 |
26 Feb 07 |
jari |
95 |
*/ |
2 |
26 Feb 07 |
jari |
96 |
protected String getCurrentElement() { |
2 |
26 Feb 07 |
jari |
97 |
return(String)m_elements.getLast(); |
2 |
26 Feb 07 |
jari |
98 |
} |
2 |
26 Feb 07 |
jari |
99 |
|
2 |
26 Feb 07 |
jari |
100 |
/** |
2 |
26 Feb 07 |
jari |
* Converts an error into a <code>SAXException</code>. |
2 |
26 Feb 07 |
jari |
102 |
*/ |
2 |
26 Feb 07 |
jari |
103 |
protected void processError( Exception ex ) throws SAXException { |
2 |
26 Feb 07 |
jari |
104 |
throw new SAXException( new ParserException("Processing error", ex) ); |
2 |
26 Feb 07 |
jari |
105 |
} |
2 |
26 Feb 07 |
jari |
106 |
} |
2 |
26 Feb 07 |
jari |
107 |
|