|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectgroovyx.net.http.ParserRegistry
public class ParserRegistry
Keeps track of response parsers for each content type. Each parser
should should be a closure that accepts an HttpResponse instance,
and returns whatever handler is appropriate for reading the response
data for that content-type. For example, a plain-text response should
probably be parsed with a Reader, while an XML response
might be parsed by an XmlSlurper, which would then be passed to the
response closure.
ContentType| Field Summary | |
|---|---|
protected Closure |
defaultParser
|
protected org.apache.commons.logging.Log |
log
|
protected Map<String,Closure> |
registeredParsers
|
| Constructor Summary | |
|---|---|
ParserRegistry()
|
|
| Method Summary | |
|---|---|
protected Map<String,Closure> |
buildDefaultParserMap()
Returns a map of default parsers. |
protected String |
getCharset(HttpResponse resp)
Get the charset from the response |
protected String |
getContentType(HttpResponse resp)
Get the content-type string from the response (no charset) |
Map<String,String> |
parseForm(HttpResponse resp)
Default parser used to decode a URL-encoded response. |
GPathResult |
parseHTML(HttpResponse resp)
Parse an HTML document by passing it through the NekoHTML parser. |
JSON |
parseJSON(HttpResponse resp)
Default parser used to decode a JSON response. |
InputStream |
parseStream(HttpResponse resp)
Default parser used for binary data. |
Reader |
parseText(HttpResponse resp)
Default parser used to handle plain text data. |
GPathResult |
parseXML(HttpResponse resp)
Default parser used to decode an XML response. |
void |
register(String contentType,
Closure closure)
Register a new parser for the given content-type. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected Closure defaultParser
protected final org.apache.commons.logging.Log log
protected Map<String,Closure> registeredParsers
| Constructor Detail |
|---|
public ParserRegistry()
| Method Detail |
|---|
protected String getCharset(HttpResponse resp)
resp - protected String getContentType(HttpResponse resp)
resp -
public InputStream parseStream(HttpResponse resp)
throws IOException
resp -
IllegalStateException
IOException
public Reader parseText(HttpResponse resp)
throws IOException
resp -
UnsupportedEncodingException
IllegalStateException
IOException
public Map<String,String> parseForm(HttpResponse resp)
throws IOException
resp -
IOException
public GPathResult parseHTML(HttpResponse resp)
throws IOException,
SAXException
resp - HTTP response from which to parse content
GPathResult from calling XmlSlurper.parse(Reader)
IOException
SAXExceptionSAXParser,
XmlSlurper.parse(Reader)
public GPathResult parseXML(HttpResponse resp)
throws IOException,
SAXException,
ParserConfigurationException
resp - HTTP response from which to parse content
GPathResult from calling XmlSlurper.parse(Reader)
IOException
SAXException
ParserConfigurationExceptionXmlSlurper.parse(Reader)
public JSON parseJSON(HttpResponse resp)
throws IOException
resp -
IOException
public void register(String contentType,
Closure closure)
HttpResponse argument and return a type suitable
to be passed to a response handler.
The value returned from the parser closure is always the second parameter
of the response handler closure.
contentType - content-type stringclosure - code that will parse the HttpResponse and return parsed
data to the response handler.protected Map<String,Closure> buildDefaultParserMap()
super.buildDefaultParserMap() and then add or remove
from that result as well.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||