public class ParserRegistry extends Object
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.
Note that all methods in this class assume HttpResponse.getEntity()
return a non-null value. It is the job of the HTTPBuilder instance to ensure
a NullPointerException is not thrown by passing a response that contains no
entity.
You can see the list of content-type parsers that are built-in to the
ParserRegistry class in buildDefaultParserMap().
ContentType| Modifier and Type | Field and Description |
|---|---|
protected static CatalogResolver |
catalogResolver
This CatalogResolver is static to avoid the overhead of re-parsing
the catalog definition file every time.
|
static String |
DEFAULT_CHARSET
The default charset to use when no charset is given in the Content-Type
header of a response.
|
protected Closure |
DEFAULT_PARSER
The default parser used for unregistered content-types.
|
protected static org.apache.commons.logging.Log |
log |
| Constructor and Description |
|---|
ParserRegistry() |
| Modifier and Type | Method and Description |
|---|---|
static void |
addCatalog(URL catalogLocation)
Add a new XML catalog definiton to the static XML resolver catalog.
|
protected Map<String,Closure> |
buildDefaultParserMap()
Returns a map of default parsers.
|
Closure |
getAt(Object contentType)
Retrieve a parser for the given response content-type string.
|
static CatalogResolver |
getCatalogResolver()
Access the default catalog used by all HTTPBuilder instances.
|
static String |
getCharset(HttpResponse resp)
Helper method to get the charset from the response.
|
static String |
getContentType(HttpResponse resp)
Helper method to get the content-type string from the response
(no charset).
|
Closure |
getDefaultParser()
Get the default parser used for unregistered content-types.
|
Iterator<Map.Entry<String,Closure>> |
iterator()
Iterate over the entire parser map
|
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.
|
Object |
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.
|
Closure |
propertyMissing(Object key)
Alias for
getAt(Object) to allow property-style access. |
void |
propertyMissing(Object key,
Closure value)
Alias for
putAt(Object, Closure) to allow property-style access. |
void |
putAt(Object contentType,
Closure value)
Register a new parser for the given content-type.
|
static void |
setDefaultCharset(String charset)
Set the charset to use for parsing character streams when no charset
is given in the Content-Type header.
|
void |
setDefaultParser(Closure defaultParser)
Set the default parser used for unregistered content-types.
|
protected final Closure DEFAULT_PARSER
parseStream(HttpResponse), which is like a no-op that just
returns the unaltered response stream.public static final String DEFAULT_CHARSET
setDefaultCharset(String).protected static final org.apache.commons.logging.Log log
protected static CatalogResolver catalogResolver
Catalog class is technically not thread-safe, but as long as you
do not parse catalog files while using the resolver, it should be fine.public ParserRegistry()
public static void setDefaultCharset(String charset)
charset - the charset to use, or null to use
DEFAULT_CHARSETpublic static String getCharset(HttpResponse resp)
Reader reader = new InputStreamReader( resp.getEntity().getContent(), ParserRegistry.getCharset( resp ) );
resp - public static String getContentType(HttpResponse resp)
resp - public InputStream parseStream(HttpResponse resp) throws IOException
resp - IllegalStateExceptionIOExceptionContentType.BINARY,
HttpEntity.getContent()public Reader parseText(HttpResponse resp) throws IOException
resp - UnsupportedEncodingExceptionIllegalStateExceptionIOExceptionContentType.TEXTpublic Map<String,String> parseForm(HttpResponse resp) throws IOException
resp - IOExceptionContentType.URLENCpublic GPathResult parseHTML(HttpResponse resp) throws IOException, SAXException
resp - HTTP response from which to parse contentGPathResult from calling XmlSlurper.parse(Reader)IOExceptionSAXExceptionContentType.HTML,
org.cyberneko.html.parsers.SAXParser,
XmlSlurper.parse(Reader)public GPathResult parseXML(HttpResponse resp) throws IOException, SAXException, ParserConfigurationException
resp - HTTP response from which to parse contentGPathResult from calling XmlSlurper.parse(Reader)IOExceptionSAXExceptionParserConfigurationExceptionContentType.XML,
XmlSlurper.parse(Reader)public Object parseJSON(HttpResponse resp) throws IOException
resp - IOExceptionContentType.JSONprotected Map<String,Closure> buildDefaultParserMap()
Returns a map of default parsers. Override this method to change
what parsers are registered by default. A 'parser' is really just a
closure that acceipts an HttpResponse instance and returns
some parsed data. You can of course call
super.buildDefaultParserMap() and then add or remove
from that result as well.
Default registered parsers are:
public static void addCatalog(URL catalogLocation) throws IOException
catalogLocation - URL of a catalog definition fileIOException - if the given URL cannot be parsed or accessed for whatever reason.public static CatalogResolver getCatalogResolver()
CatalogResolver instancepublic Closure getDefaultParser()
public void setDefaultParser(Closure defaultParser)
defaultParser - ifpublic Closure getAt(Object contentType)
contentType - public void putAt(Object contentType, Closure value)
HttpResponse argument and return a type suitable
to be passed as the 'parsed data' argument of a
response handler closure.contentType - content-type stringvalue - code that will parse the HttpResponse and return parsed
data to the response handler.public Closure propertyMissing(Object key)
getAt(Object) to allow property-style access.key - content-type stringpublic void propertyMissing(Object key, Closure value)
putAt(Object, Closure) to allow property-style access.key - content-type stringvalue - parser closureCopyright © 2008-2014. All Rights Reserved.