org.cauldron.tests.util.DigesterContext.java Source code

Java tutorial

Introduction

Here is the source code for org.cauldron.tests.util.DigesterContext.java

Source

/*
 * Copyright (c) 2005 Jonathan Ross <jonross@alum.mit.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package org.cauldron.tests.util;

import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.bind.Validator;
import javax.xml.bind.helpers.AbstractUnmarshallerImpl;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSet;
import org.apache.commons.digester.xmlrules.FromXmlRuleSet;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

/**
 * Digester-based JAXB context for JAXB task testing.
 */

public class DigesterContext extends JAXBContext {
    private RuleSet rules;

    public static JAXBContext createContext(String path, ClassLoader cl) throws JAXBException {
        URL url = cl.getResource(path.replace('.', '/') + "/rules.xml");
        DigesterContext dc = new DigesterContext();
        dc.rules = new FromXmlRuleSet(url);
        return dc;
    }

    public Marshaller createMarshaller() throws JAXBException {
        throw new JAXBException("Marshalling not supported by " + getClass());
    }

    public Unmarshaller createUnmarshaller() throws JAXBException {
        return new DigesterUnmarshaller(rules);
    }

    public Validator createValidator() throws JAXBException {
        throw new JAXBException("Validation not supported by " + getClass());
    }
}

class DigesterUnmarshaller extends AbstractUnmarshallerImpl {
    private DigesterHandler digester;

    DigesterUnmarshaller(RuleSet rules) {
        digester = new DigesterHandler();
        digester.addRuleSet(rules);
    }

    protected Object unmarshal(XMLReader reader, InputSource input) throws JAXBException {
        reader.setContentHandler(digester);

        try {
            reader.parse(input);
            return digester.getRoot();
        } catch (Exception e) {
            throw new JAXBException(e);
        }
    }

    public UnmarshallerHandler getUnmarshallerHandler() {
        return digester;
    }

    public Object unmarshal(Node arg0) throws JAXBException {
        throw new JAXBException("unmarshal(Node) not yet implemented");
    }
}

class DigesterHandler extends Digester implements UnmarshallerHandler {
    public Object getResult() throws JAXBException, IllegalStateException {
        return getRoot();
    }
}