|
FIELD
annotations
<table>
<thead>
<tr>
</thead>
<tbody>
<tr>
<tr>
</tbody>
</table>
<h4>METHOD
annotations
<table>
<thead>
<tr>
</thead>
<tbody>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
<h4>PARAMETER
annotations
<table>
<thead>
<tr>
</thead>
<tbody>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
</subsection>
<subsection name="Bootstrapping">
<p>The core of Digester annotations rules processor is the
<code>org.apache.commons.digester.annotations.DigesterLoader class.
<p>A org.apache.commons.digester.annotations.DigesterLoader
instance is able to analyze <code>Class<?> graphs and builds
the relative <code>org.apache.commons.digester.RuleSet to create
<code>org.apache.commons.digester.Digester instances.
<p>The bootstrap sequence has been designed to be as simple as possible,
all that's needed is creating a new
<code>org.apache.commons.digester.annotations.DigesterLoaderBuilder
instance, plugging the desired
<code>org.apache.commons.digester.annotations.spi.AnnotationRuleProviderFactory and
<code>org.apache.commons.digester.annotations.spi.DigesterLoaderHandlerFactory.
using a chaining builders pattern.</p>
<p>An org.apache.commons.digester.annotations.spi.AnnotationRuleProviderFactory
implementation performs the creation of
<code>org.apache.commons.digester.annotations.AnnotationRuleProvider<A extends Annotation, E extends AnnotatedElement, R extends Rule>
instances; the default implementation is limited to create the provider
by invoking the default empty constructor of the required class, but
users are free to give their implementation if they need a more complex
factory, i.e. providers requires components that could be injected from a
context, etc. etc.</p>
<h5>Note
<p>It is strongly descouraged caching AnnotationRuleProvider
instances!!!</p>
<p>Same thing for the org.apache.commons.digester.annotations.spi.DigesterLoaderHandlerFactory
,
which implementation performs the creation of
<code>DigesterLoaderHandler<A extends Annotation, E extends AnnotatedElement>
instances; the default implementation is limited to create the handler
by invoking the default empty constructor of the required class, but
users are free to give their implementation if they need a more complex
factory, i.e. providers requires components that could be injected from a
context, etc. etc.</p>
<p>Said that, to obtain a fresh new
<code>org.apache.commons.digester.annotations.DigesterLoader instance
with default factories, it is enough invoking the default empty constructor:</p>
<source>DigesterLoader digesterLoader = new DigesterLoaderBuilder()
.useDefaultAnnotationRuleProviderFactory()
.useDefaultDigesterLoaderHandlerFactory();</source>
<p>Otherwise, if users need specify theyr custom factories:
<source>DigesterLoader digesterLoader = new DigesterLoaderBuilder()
.useAnnotationRuleProviderFactory(new MyAnnotationRuleProviderFactory())
.useDigesterLoaderHandlerFactory(new MyDigesterLoaderHandlerFactory());</source>
</subsection>
<subsection name="Example: a simple RSS parser">
<p>Let's assume there is the need to parse the following (simplified)
XML/RSS feed:</p>
<source><rss version="2.0">
<channel>
<title>Apache</title>
<link>http://www.apache.org</link>
<description>The Apache Software Foundation</description>
<language>en-US</language>
<rating>(PICS-1.1 "http://www.rsac.org/ratingsv01.html"
2 gen true comment "RSACi North America Server"
for "http://www.rsac.org" on "1996.04.16T08:15-0500"
r (n 0 s 0 v 0 l 0))</rating>
<image>
<title>Apache</title>
<url>http://jakarta.apache.org/images/jakarta-logo.gif</url>
<link>http://jakarta.apache.org</link>
<width>505</width>
<height>480</height>
<description>The Jakarta project. Open source, serverside java.</description>
</image>
<item>
<title>Commons Attributes 2.1 Released</title>
<link>http://jakarta.apache.org/site/news/news-2004-2ndHalf.html#20040815.1</link>
<description>The Apache Commons team is happy to announce the release of Commons Attributes 2.1.
This is the first release of the new Commons-Attributes code.</description>
</item>
<item>
<title>Cloudscape Becomes Apache Derby</title>
<link>http://jakarta.apache.org/site/news/elsewhere-2004-2ndHalf.html#20040803.1</link>
<description>IBM has submitted a proposal to the Apache DB project for a Java-based package to be called 'Derby'.</description>
</item>
<item>
<title>Commons BeanUtils 1.7 Released</title>
<link>http://jakarta.apache.org/site/news/news-2004-2ndHalf.html#20040802.1</link>
<description/>
</item>
<item>
<title>Commons JXPath 1.2 Released</title>
<link>http://jakarta.apache.org/site/news/news-2004-2ndHalf.html#20040801.2</link>
<description/>
</item>
</channel>
</rss></source>
<p>So, let's define the Java entities and annotate them; first the Channel
entity:
<source>@ObjectCreate(pattern = "rss/channel")
class Channel {
private final List<Item> items = new ArrayList<Item>();
@BeanPropertySetter(pattern = "rss/channel/title")
private String title;
@BeanPropertySetter(pattern = "rss/channel/link")
private String link;
@BeanPropertySetter(pattern = "rss/channel/description")
private String description;
@BeanPropertySetter(pattern = "rss/channel/language")
private String language;
private Image image;
// getters and setters
@SetNext
public void setImage(Image image) {
this.image = image;
}
@SetNext
public void addItem(Item item) {
this.items.add(item);
}
}</source>
<p>Then the Image
entity:
<source>@ObjectCreate(pattern = "rss/channel/image")
class Image {
@BeanPropertySetter(pattern = "rss/channel/image/description")
private String description;
@BeanPropertySetter(pattern = "rss/channel/image/width")
private int width;
@BeanPropertySetter(pattern = "rss/channel/image/height")
private int height;
@BeanPropertySetter(pattern = "rss/channel/image/link")
private String link;
@BeanPropertySetter(pattern = "rss/channel/image/title")
private String title;
@BeanPropertySetter(pattern = "rss/channel/image/url")
private String url;
// getters and setters
}</source>
<p>and finally the Item
entity:
<source>@ObjectCreate(pattern = "rss/channel/item")
class Item {
@BeanPropertySetter(pattern = "rss/channel/item/description")
private String description;
@BeanPropertySetter(pattern = "rss/channel/item/link")
private String link;
@BeanPropertySetter(pattern = "rss/channel/item/title")
private String title;
// getters and setters
}</source>
<p>It is now possible to create the Digester
instance and parse the XML:
<source>DigesterLoader digesterLoader = new DigesterLoaderBuilder()
.useDefaultAnnotationRuleProviderFactory()
.useDefaultDigesterLoaderHandlerFactory();
...
Digester digester = digesterLoader.createDigester(Channel.class);
try {
Channel channel = (Channel) digester.parse(new URL("http://www.myfeedprovider.com/rss.xml").openStream());
} catch (Exception e) {
// do something
}
</source>
<h5>Notes
<p>If asking to the DigesterLoader
instance more then twice the
<code>Digester for the same Class<?>
, the
<code>DigesterLoader won't analize the target class for each request,
but rather will reuse cached results.</p>
<p>The same DigesterLoader
instance can be reused to create
other <code>Digester instances.
</subsection>
</section>
</body>
</document>
Here is a short list of links related to this Commons Digester annotations.xml source code file:
Commons Digester example source code file (annotations.xml)
The Commons Digester annotations.xml source code<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd"> <properties> <title>Download Commons Digester <author email="dev@commons.apache.org">Commons Documentation Team </properties> <body> <section name="Annotations"> <p>The | Annotation | Reflect rule | |
---|---|---|---|
@ObjectCreate | org.apache.commons.digester.ObjectCreateRule | ||
@FactoryCreate | org.apache.commons.digester.FactoryCreateRule | ||
Annotation | Reflect rule | ||
@BeanPropertySetter | org.apache.commons.digester.BeanPropertySetterRule | ||
@SetProperty | org.apache.commons.digester.SetPropertiesRule | ||
Annotation | Reflect rule | ||
@CallMethod | org.apache.commons.digester.CallMethodRule | ||
@SetNext | org.apache.commons.digester.SetNextRule | ||
@SetRoot | org.apache.commons.digester.SetRootRule | ||
@SetTop | org.apache.commons.digester.SetTopRule | ||
Annotation | Reflect rule | ||
@AttributeCallParam | org.apache.commons.digester.Digester#addCallParam(String, int, String) | ||
@CallParam | org.apache.commons.digester.Digester#addCallParam(String, int) | ||
@PathCallParam | org.apache.commons.digester.Digester#addCallParamPath(String, int) | ||
@StackCallParam | org.apache.commons.digester.Digester#addCallParam(String, int, int) |
... this post is sponsored by my books ... | |
![]() #1 New Release! |
![]() FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.