Coverage Report - javax.faces.context.PartialResponseWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PartialResponseWriter
0%
0/101
0%
0/12
1.364
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package javax.faces.context;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * @since 2.0
 27  
  */
 28  0
 public class PartialResponseWriter extends ResponseWriterWrapper
 29  
 {
 30  
     public static final String RENDER_ALL_MARKER = "javax.faces.ViewRoot";
 31  
     public static final String VIEW_STATE_MARKER = "javax.faces.ViewState";
 32  
 
 33  
     private ResponseWriter _wrapped;
 34  
     private boolean hasChanges;
 35  
     private String insertType;
 36  
 
 37  
    
 38  
     /**
 39  
      * 
 40  
      */
 41  
     public PartialResponseWriter(ResponseWriter writer)
 42  0
     {
 43  0
         _wrapped = writer;
 44  0
     }
 45  
 
 46  
     public void delete(String targetId) throws IOException
 47  
     {
 48  0
         startChanges();
 49  
         
 50  0
         _wrapped.startElement ("delete", null);
 51  0
         _wrapped.writeAttribute ("id", targetId, null);
 52  0
         _wrapped.endElement ("delete");
 53  0
     }
 54  
 
 55  
     /**
 56  
      * {@inheritDoc}
 57  
      */
 58  
     @Override
 59  
     public void endDocument() throws IOException
 60  
     {
 61  0
         if (hasChanges)
 62  
         {
 63  
             // Close the <insert> element, if any.
 64  
             //error close the last op if any
 65  0
             endInsert();
 66  
             
 67  0
             _wrapped.endElement ("changes");
 68  
             
 69  0
             hasChanges = false;
 70  
         }
 71  
         
 72  0
         _wrapped.endElement ("partial-response");
 73  0
     }
 74  
 
 75  
     public void endError() throws IOException
 76  
     {
 77  
         // Close open <error-message> element.
 78  
         
 79  0
         _wrapped.endCDATA();
 80  0
         _wrapped.endElement ("error-message");
 81  0
         _wrapped.endElement ("error");
 82  0
     }
 83  
 
 84  
     public void endEval() throws IOException
 85  
     {
 86  
         // Close open <eval> element.
 87  
         
 88  0
         _wrapped.endCDATA();
 89  0
         _wrapped.endElement ("eval");
 90  0
     }
 91  
 
 92  
     public void endExtension() throws IOException
 93  
     {
 94  0
         _wrapped.endElement ("extension");
 95  0
     }
 96  
 
 97  
     public void endInsert() throws IOException
 98  
     {
 99  0
         if (insertType == null)
 100  
         {
 101  
             // No insert started; ignore.
 102  
             
 103  0
             return;
 104  
         }
 105  
         
 106  
         // Close open <insert> element.
 107  
         
 108  0
         _wrapped.endCDATA();
 109  0
         _wrapped.endElement (insertType);
 110  0
         _wrapped.endElement ("insert");
 111  
         
 112  0
         insertType = null;
 113  0
     }
 114  
 
 115  
     public void endUpdate() throws IOException
 116  
     {
 117  0
         _wrapped.endCDATA();
 118  0
         _wrapped.endElement ("update");
 119  0
     }
 120  
 
 121  
     /**
 122  
      * {@inheritDoc}
 123  
      */
 124  
     @Override
 125  
     public ResponseWriter getWrapped()
 126  
     {
 127  0
         return _wrapped;
 128  
     }
 129  
 
 130  
     public void redirect(String url) throws IOException
 131  
     {
 132  0
         _wrapped.startElement ("redirect", null);
 133  0
         _wrapped.writeAttribute ("url", url, null);
 134  0
         _wrapped.endElement ("redirect");
 135  0
     }
 136  
 
 137  
     /**
 138  
      * {@inheritDoc}
 139  
      */
 140  
     @Override
 141  
     public void startDocument() throws IOException
 142  
     {
 143  
         // JSF 2.2 section 2.2.6.1 Render Response Partial Processing
 144  
         // use writePreamble(...)
 145  
         //_wrapped.write ("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
 146  
         
 147  0
         _wrapped.startElement ("partial-response", null);
 148  
         
 149  
         // If by some reason the response has been reset, and the same
 150  
         // PartialResponseWriter is used, it is necessary to ensure any 
 151  
         // variable is initialized in a consistent state. To do that,
 152  
         // the best point is when the document is started.
 153  0
         hasChanges = false;
 154  0
         insertType = null;
 155  0
     }
 156  
 
 157  
     public void startError(String errorName) throws IOException
 158  
     {
 159  0
         _wrapped.startElement ("error", null);
 160  
         
 161  0
         _wrapped.startElement ("error-name", null);
 162  0
         _wrapped.write (errorName);
 163  0
         _wrapped.endElement ("error-name");
 164  
         
 165  0
         _wrapped.startElement ("error-message", null);
 166  0
         startCDATA();
 167  
         
 168  
         // Leave open; caller will write message.
 169  0
     }
 170  
 
 171  
     @Override
 172  
     public void startCDATA() throws IOException
 173  
     {
 174  0
         _wrapped.startCDATA();
 175  0
     }
 176  
 
 177  
     @Override
 178  
     public void endCDATA() throws IOException
 179  
     {
 180  0
         _wrapped.endCDATA();    
 181  0
     }
 182  
 
 183  
     public void startEval() throws IOException
 184  
     {
 185  0
         startChanges();
 186  
         
 187  0
         _wrapped.startElement ("eval", null);
 188  0
         startCDATA();
 189  
         
 190  
         // Leave open; caller will write statements.
 191  0
     }
 192  
 
 193  
     public void startExtension(Map<String, String> attributes) throws IOException
 194  
     {
 195  
         Iterator<String> attrNames;
 196  
         
 197  0
         startChanges();
 198  
         
 199  0
         _wrapped.startElement ("extension", null);
 200  
         
 201  
         // Write out extension attributes.
 202  
         // TODO: schema mentions "id" attribute; not used?
 203  
         
 204  0
         attrNames = attributes.keySet().iterator();
 205  
         
 206  0
         while (attrNames.hasNext())
 207  
         {
 208  0
             String attrName = attrNames.next();
 209  
             
 210  0
             _wrapped.writeAttribute (attrName, attributes.get (attrName), null);
 211  0
         }
 212  
         
 213  
         // Leave open; caller will write extension elements.
 214  0
     }
 215  
 
 216  
     public void startInsertAfter(String targetId) throws IOException
 217  
     {
 218  0
         startInsertCommon ("after", targetId);
 219  0
     }
 220  
 
 221  
     public void startInsertBefore(String targetId) throws IOException
 222  
     {
 223  0
         startInsertCommon ("before", targetId);
 224  0
     }
 225  
 
 226  
     public void startUpdate(String targetId) throws IOException
 227  
     {
 228  0
         startChanges();
 229  
         
 230  0
         _wrapped.startElement ("update", null);
 231  0
         _wrapped.writeAttribute ("id", targetId, null);
 232  0
         startCDATA();
 233  
         
 234  
         // Leave open; caller will write content.
 235  0
     }
 236  
 
 237  
     public void updateAttributes(String targetId, Map<String, String> attributes) throws IOException
 238  
     {
 239  
         Iterator<String> attrNames;
 240  
         
 241  0
         startChanges();
 242  
         
 243  0
         _wrapped.startElement ("attributes", null);
 244  0
         _wrapped.writeAttribute ("id", targetId, null);
 245  
         
 246  0
         attrNames = attributes.keySet().iterator();
 247  
         
 248  0
         while (attrNames.hasNext())
 249  
         {
 250  0
             String attrName = attrNames.next();
 251  
             
 252  0
             _wrapped.startElement ("attribute", null);
 253  0
             _wrapped.writeAttribute ("name", attrName, null);
 254  0
             _wrapped.writeAttribute ("value", attributes.get (attrName), null);
 255  0
             _wrapped.endElement ("attribute");
 256  0
         }
 257  
         
 258  0
         _wrapped.endElement ("attributes");
 259  0
     }
 260  
     
 261  
     private void startChanges () throws IOException
 262  
     {
 263  0
         if (!hasChanges)
 264  
         {
 265  0
             _wrapped.startElement ("changes", null);
 266  
             
 267  0
             hasChanges = true;
 268  
         }
 269  0
     }
 270  
     
 271  
     private void startInsertCommon (String type, String targetId) throws IOException
 272  
     {
 273  0
         if (insertType != null)
 274  
         {
 275  
             // An insert has already been started; ignore.
 276  
             
 277  0
             return;
 278  
         }
 279  
         
 280  0
         insertType = type;
 281  
         
 282  0
         startChanges();
 283  
         
 284  0
         _wrapped.startElement ("insert", null);
 285  0
         _wrapped.startElement (insertType, null);
 286  0
         _wrapped.writeAttribute ("id", targetId, null);
 287  0
         startCDATA();
 288  
         
 289  
         // Leave open; caller will write content.
 290  0
     }
 291  
 }