Package de.danet.an.workflow.clients.mgmtportlets.util.jsf

Source Code of de.danet.an.workflow.clients.mgmtportlets.util.jsf.SaxEventBufferConverter

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: SaxEventBufferConverter.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.2  2006/09/08 11:20:40  drmlipp
* Improved error messages.
*
* Revision 1.1  2006/09/07 15:02:57  drmlipp
* Added.
*
*/
package de.danet.an.workflow.clients.mgmtportlets.util.jsf;

import java.io.IOException;
import java.io.StringReader;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

import de.danet.an.util.jsf.JSFUtil;
import de.danet.an.util.sax.SAXContentBuffer;
import de.danet.an.util.sax.XmlnsUrisPatcher;
import de.danet.an.workflow.util.SAXEventBufferImpl;

/**
* This class provides a converter for the <code>SAXEventBuffer</code> type.
*
* @author Michael Lipp
*/
public class SaxEventBufferConverter implements Converter {

    /* (non-Javadoc)
     * @see javax.faces.convert.Converter#getAsObject
     */
    public Object getAsObject
        (FacesContext facesContext, UIComponent component, String value)
        throws ConverterException {
        try {
            SAXParserFactory pf = SAXParserFactory.newInstance();
            pf.setValidating (false);
            pf.setNamespaceAware (true);
            pf.setFeature
                ("http://xml.org/sax/features/namespace-prefixes", true);
            XMLReader reader = null;
            try {
                pf.setFeature ("http://xml.org/sax/features/xmlns-uris", true);
                SAXParser parser = pf.newSAXParser();
                reader = parser.getXMLReader();
            } catch (SAXException e) {
                SAXParser parser = pf.newSAXParser();
                reader = new XmlnsUrisPatcher (parser.getXMLReader());
            }
            SAXEventBufferImpl res = new SAXEventBufferImpl ();
            // Create temporary root element to make sure
            // that structure is well formed.
            XMLFilterImpl filter = new XMLFilterImpl () {
                private int level = 0;

                public void startElement
                    (String uri, String localName, String qName,
                            Attributes atts) throws SAXException {
                    if (level > 0) {
                        super.startElement (uri,localName,qName,atts);
                    }
                    level += 1;
                }

                public void endElement
                    (String uri, String localName, String qName)
                throws SAXException{
                    level -= 1;
                    if (level > 0) {
                        super.endElement (uri, localName, qName);
                    }
                }
            };
            filter.setContentHandler (res);
            reader.setContentHandler (filter);
            InputSource inSrc = new InputSource
                (new StringReader
                 ("<temporary-root>" + value + "</temporary-root>"));
            reader.parse(inSrc);
            return res;
        } catch (ParserConfigurationException e) {
            throw new ConverterException (e);
        } catch (IOException e) {
            throw new ConverterException (e);
        } catch (SAXException e) {
            String summary = JSFUtil.getApplicationMessage
                ("javax.faces.component.UIInput.CONVERSION");
            throw new ConverterException
                (new FacesMessage
                 (FacesMessage.SEVERITY_ERROR, summary, e.getMessage()));
        }
    }

    /* (non-Javadoc)
     * @see javax.faces.convert.Converter#getAsString
     */
    public String getAsString
        (FacesContext facesContext, UIComponent component, Object value)
        throws ConverterException {
        return ((SAXContentBuffer)value).toString(true);
    }

}
TOP

Related Classes of de.danet.an.workflow.clients.mgmtportlets.util.jsf.SaxEventBufferConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.