FileDocCategorySizeDatePackage
AccountBuilder.javaAPI DocApache Lucene 2.1.03454Wed Feb 14 10:46:06 GMT 2007org.apache.lucene.gdata.server.administration

AccountBuilder

public class AccountBuilder extends Object
Helper class to create {@link org.apache.lucene.gdata.data.GDataAccount} instances from a xml stream provided via a {@link Reader} instance.
author
Simon Willnauer

Fields Summary
Constructors Summary
Methods Summary
public static org.apache.lucene.gdata.data.GDataAccountbuildAccount(java.io.Reader reader)
Reads the xml from the provided reader and binds the values to the

param
reader - the reader to read the xml from
return
- the GDataAccount
throws
IOException - if an IOException occurs
throws
SAXException - if the xml can not be parsed by the sax reader

        if (reader == null)
            throw new IllegalArgumentException("Reader must not be null");
        URL resource = AccountBuilder.class.getResource("/gdata-account.xsd");
        if(resource == null)
            throw new RuntimeException("can not find xml schema file 'gdata-account.xsd' -- file must be present on the classpath");
        String schemaFile = resource.getFile();
        GDataAccount account = null;
        /*
         * Force using apache xerces parser for digester
         */
        SAXParser parser = new SAXParser();
        parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",true);
        parser.setFeature("http://apache.org/xml/features/validation/schema",true);
        parser.setFeature("http://xml.org/sax/features/validation",true); 
        parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",schemaFile);
        Digester digester = new Digester(parser);
        digester.setValidating(true);
        digester.setErrorHandler(new SimpleSaxErrorHandler());
        digester.setSchema(schemaFile);
        digester.addObjectCreate("account", GDataAccount.class);
        digester.addBeanPropertySetter("account/account-name", "name");
        digester.addBeanPropertySetter("account/password", "password");
        digester.addBeanPropertySetter("account/account-role", "rolesAsInt");
        digester.addBeanPropertySetter("account/account-owner/name",
                "authorname");
        digester.addBeanPropertySetter("account/account-owner/email-address",
                "authorMail");
        digester.addBeanPropertySetter("account/account-owner/url",
                "authorLink");

        account = (GDataAccount) digester.parse(reader);
        return account;