All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.faces.context.PartialResponseWriter Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
/*
 * 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.
 */
package javax.faces.context;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Simon Lessard (latest modification by $Author: lu4242 $)
 * @version $Revision: 1401353 $ $Date: 2012-10-23 12:16:54 -0500 (Tue, 23 Oct 2012) $
 * 
 * @since 2.0
 */
public class PartialResponseWriter extends ResponseWriterWrapper
{
    public static final String RENDER_ALL_MARKER = "javax.faces.ViewRoot";
    public static final String VIEW_STATE_MARKER = "javax.faces.ViewState";

    private ResponseWriter _wrapped;
    private boolean hasChanges;
    private String insertType;

   
    /**
     * 
     */
    public PartialResponseWriter(ResponseWriter writer)
    {
        _wrapped = writer;
    }

    public void delete(String targetId) throws IOException
    {
        startChanges();
        
        _wrapped.startElement ("delete", null);
        _wrapped.writeAttribute ("id", targetId, null);
        _wrapped.endElement ("delete");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void endDocument() throws IOException
    {
        if (hasChanges)
        {
            // Close the  element, if any.
            //error close the last op if any
            endInsert();
            
            _wrapped.endElement ("changes");
            
            hasChanges = false;
        }
        
        _wrapped.endElement ("partial-response");
    }

    public void endError() throws IOException
    {
        // Close open  element.
        
        _wrapped.endCDATA();
        _wrapped.endElement ("error-message");
        _wrapped.endElement ("error");
    }

    public void endEval() throws IOException
    {
        // Close open  element.
        
        _wrapped.endCDATA();
        _wrapped.endElement ("eval");
    }

    public void endExtension() throws IOException
    {
        _wrapped.endElement ("extension");
    }

    public void endInsert() throws IOException
    {
        if (insertType == null)
        {
            // No insert started; ignore.
            
            return;
        }
        
        // Close open  element.
        
        _wrapped.endCDATA();
        _wrapped.endElement (insertType);
        _wrapped.endElement ("insert");
        
        insertType = null;
    }

    public void endUpdate() throws IOException
    {
        _wrapped.endCDATA();
        _wrapped.endElement ("update");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseWriter getWrapped()
    {
        return _wrapped;
    }

    public void redirect(String url) throws IOException
    {
        _wrapped.startElement ("redirect", null);
        _wrapped.writeAttribute ("url", url, null);
        _wrapped.endElement ("redirect");
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void startDocument() throws IOException
    {
        _wrapped.write ("");
        
        _wrapped.startElement ("partial-response", null);
        
        // If by some reason the response has been reset, and the same
        // PartialResponseWriter is used, it is necessary to ensure any 
        // variable is initialized in a consistent state. To do that,
        // the best point is when the document is started.
        hasChanges = false;
        insertType = null;
    }

    public void startError(String errorName) throws IOException
    {
        _wrapped.startElement ("error", null);
        
        _wrapped.startElement ("error-name", null);
        _wrapped.write (errorName);
        _wrapped.endElement ("error-name");
        
        _wrapped.startElement ("error-message", null);
        startCDATA();
        
        // Leave open; caller will write message.
    }

    @Override
    public void startCDATA() throws IOException
    {
        _wrapped.startCDATA();
    }

    @Override
    public void endCDATA() throws IOException
    {
        _wrapped.endCDATA();    
    }

    public void startEval() throws IOException
    {
        startChanges();
        
        _wrapped.startElement ("eval", null);
        startCDATA();
        
        // Leave open; caller will write statements.
    }

    public void startExtension(Map attributes) throws IOException
    {
        Iterator attrNames;
        
        startChanges();
        
        _wrapped.startElement ("extension", null);
        
        // Write out extension attributes.
        // TODO: schema mentions "id" attribute; not used?
        
        attrNames = attributes.keySet().iterator();
        
        while (attrNames.hasNext())
        {
            String attrName = attrNames.next();
            
            _wrapped.writeAttribute (attrName, attributes.get (attrName), null);
        }
        
        // Leave open; caller will write extension elements.
    }

    public void startInsertAfter(String targetId) throws IOException
    {
        startInsertCommon ("after", targetId);
    }

    public void startInsertBefore(String targetId) throws IOException
    {
        startInsertCommon ("before", targetId);
    }

    public void startUpdate(String targetId) throws IOException
    {
        startChanges();
        
        _wrapped.startElement ("update", null);
        _wrapped.writeAttribute ("id", targetId, null);
        startCDATA();
        
        // Leave open; caller will write content.
    }

    public void updateAttributes(String targetId, Map attributes) throws IOException
    {
        Iterator attrNames;
        
        startChanges();
        
        _wrapped.startElement ("attributes", null);
        _wrapped.writeAttribute ("id", targetId, null);
        
        attrNames = attributes.keySet().iterator();
        
        while (attrNames.hasNext())
        {
            String attrName = attrNames.next();
            
            _wrapped.startElement ("attribute", null);
            _wrapped.writeAttribute ("name", attrName, null);
            _wrapped.writeAttribute ("value", attributes.get (attrName), null);
            _wrapped.endElement ("attribute");
        }
        
        _wrapped.endElement ("attributes");
    }
    
    private void startChanges () throws IOException
    {
        if (!hasChanges)
        {
            _wrapped.startElement ("changes", null);
            
            hasChanges = true;
        }
    }
    
    private void startInsertCommon (String type, String targetId) throws IOException
    {
        if (insertType != null)
        {
            // An insert has already been started; ignore.
            
            return;
        }
        
        insertType = type;
        
        startChanges();
        
        _wrapped.startElement ("insert", null);
        _wrapped.startElement (insertType, null);
        _wrapped.writeAttribute ("id", targetId, null);
        startCDATA();
        
        // Leave open; caller will write content.
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy