/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003,2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.impl;
import javax.xml.bind.DatatypeConverterInterface;
import javax.xml.bind.JAXBException;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.ValidationEventLocator;
import javax.xml.bind.helpers.ValidationEventLocatorImpl;
import org.apache.ws.jaxme.JMHandler;
import org.apache.ws.jaxme.JMUnmarshaller;
import org.apache.ws.jaxme.Observer;
import org.apache.ws.jaxme.ValidationEvents;
import org.apache.ws.jaxme.util.NamespaceSupport;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/** <p>A common base class for the AtomicHandler and the
* JMHandlerImpl.</p>
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
* @version $Id: JMHandlerBase.java 231705 2004-01-07 02:07:20Z jochen $
*/
public abstract class JMHandlerBase implements JMHandler {
private Locator locator;
private JMHandler.Data data;
private Observer observer;
protected class DataImpl implements JMHandler.Data {
private JMUnmarshaller unmarshaller;
private AtomicHandler atomicHandler;
private NamespaceSupport namespaceSupport = new NamespaceSupport();
public DataImpl(JMUnmarshaller pUnmarshaller) {
unmarshaller = pUnmarshaller;
}
public JMUnmarshaller getUnmarshaller() {
return unmarshaller;
}
public JAXBContextImpl getFactory() {
return unmarshaller.getJAXBContextImpl();
}
public JMHandler getAtomicHandler() {
if (atomicHandler == null) {
atomicHandler = new AtomicHandler();
}
return atomicHandler;
}
public DatatypeConverterInterface getDatatypeConverter() {
return unmarshaller.getDatatypeConverter();
}
public NamespaceSupport getNamespaceContext() {
return namespaceSupport;
}
}
public void init(JMUnmarshaller pUnmarshaller) throws JAXBException {
init(new DataImpl(pUnmarshaller));
}
public void init(JMHandler.Data pData) throws JAXBException {
data = pData;
}
public JMHandler.Data getData() {
return data;
}
public void setDocumentLocator(Locator pLocator) {
locator = pLocator;
}
/** <p>Returns the Locator previously set by <code>setDocumentLocator()</code>,
* if any.</p>
*/
public Locator getDocumentLocator() {
return locator;
}
public void setObserver(Observer pObserver) {
observer = pObserver;
}
public Observer getObserver() {
return observer;
}
protected void validationEvent(int pSeverity, String pMsg, String pErrorCode)
throws SAXException {
ValidationEventHandler eventHandler;
try {
eventHandler = getData().getUnmarshaller().getEventHandler();
} catch (JAXBException e) {
/* Interesting question: Which event to report? The JAXBException or the
* validation event? We choose the former.
*/
throw new SAXException(e);
}
if (eventHandler == null) {
throw new SAXParseException(pMsg, getDocumentLocator());
} else {
ValidationEventLocator myLocator = new ValidationEventLocatorImpl(getDocumentLocator());
ValidationEventImpl event = new ValidationEventImpl(pSeverity,
pErrorCode + ": " + pMsg,
myLocator);
event.setErrorCode(pErrorCode);
eventHandler.handleEvent(event);
}
}
protected void validationEvent(int pSeverity, String pMsg, String pErrorCode,
Exception pException)
throws SAXException {
ValidationEventHandler eventHandler;
try {
eventHandler = getData().getUnmarshaller().getEventHandler();
} catch (JAXBException e) {
/* Interesting question: Which event to report? The JAXBException or the
* validation event? We decide against the former.
*/
throw new SAXException(e);
}
if (eventHandler == null) {
throw new SAXParseException(pMsg, getDocumentLocator(), pException);
} else {
ValidationEventLocator myLocator = new ValidationEventLocatorImpl(getDocumentLocator());
ValidationEventImpl event = new ValidationEventImpl(pSeverity,
pErrorCode + ": " + pMsg,
myLocator,
pException);
event.setErrorCode(pErrorCode);
eventHandler.handleEvent(event);
}
}
public void processingInstruction(String pTarget, String pData)
throws SAXException {
validationEvent(ValidationEvent.WARNING,
"Don't know how to handle processing instructions.",
ValidationEvents.EVENT_PROCESSING_INSTRUCTION);
}
public void skippedEntity(String name) throws SAXException {
validationEvent(ValidationEvent.WARNING,
"Don't know how to handle skipped entities.",
ValidationEvents.EVENT_SKIPPED_ENTITY);
}
public void startPrefixMapping(String pPrefix, String pURI)
throws SAXException {
getData().getNamespaceContext().declarePrefix(pPrefix, pURI);
}
public void endPrefixMapping(String pPrefix) throws SAXException {
getData().getNamespaceContext().undeclarePrefix(pPrefix);
}
public void endDocument() {
Observer o = getObserver();
if (o != null) {
o.notify(getResult());
}
}
}