Coverage Report - org.apache.commons.betwixt.io.read.MappingAction

Classes in this File Line Coverage Branch Coverage Complexity
MappingAction
100% 
100% 
1.083

 1  
 /*
 2  
  * Copyright 2004 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */ 
 16  
 package org.apache.commons.betwixt.io.read;
 17  
 
 18  
 import org.apache.commons.betwixt.AttributeDescriptor;
 19  
 import org.apache.commons.betwixt.ElementDescriptor;
 20  
 import org.xml.sax.Attributes;
 21  
 
 22  
 /**
 23  
  * Executes mapping action for a subgraph.
 24  
  * It is intended that most MappingAction's will not need to maintain state.
 25  
  * 
 26  
  * @author <a href='http://jakarta.apache.org/'>Apache Commons Team</a>
 27  
  * @version $Revision: 397646 $
 28  
  */
 29  2598
 public abstract class MappingAction {
 30  
 
 31  
        
 32  
     public abstract MappingAction next(
 33  
         String namespace,
 34  
         String name,
 35  
         Attributes attributes,
 36  
         ReadContext context)
 37  
         throws Exception;
 38  
 
 39  
     /**
 40  
      * Executes mapping action on new element.
 41  
      * @param namespace
 42  
      * @param name
 43  
      * @param attributes Attributes not null
 44  
      * @param context Context not null
 45  
      * @return the MappingAction to be used to map the sub-graph 
 46  
      * under this element
 47  
      * @throws Exception
 48  
      */
 49  
     public abstract MappingAction begin(
 50  
         String namespace,
 51  
         String name,
 52  
         Attributes attributes,
 53  
         ReadContext context)
 54  
         throws Exception;
 55  
 
 56  
     /**
 57  
      * Executes mapping action for element body text
 58  
      * @param text
 59  
      * @param context
 60  
      * @throws Exception
 61  
      */
 62  
     public abstract void body(String text, ReadContext context)
 63  
         throws Exception;
 64  
 
 65  
     /**
 66  
      * Executes mapping action one element ends
 67  
      * @param context
 68  
      * @throws Exception
 69  
      */
 70  
     public abstract void end(ReadContext context) throws Exception;
 71  
 
 72  487
     public static final MappingAction EMPTY = new MappingAction.Base();
 73  
 
 74  487
     public static final MappingAction IGNORE = new MappingAction.Ignore();    
 75  
     
 76  487
     private static final class Ignore extends MappingAction {
 77  
 
 78  
         public MappingAction next(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
 79  39
             return this;
 80  
         }
 81  
 
 82  
         public MappingAction begin(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
 83  52
             return this;
 84  
         }
 85  
 
 86  
         public void body(String text, ReadContext context) throws Exception {
 87  
             // do nothing
 88  52
         }
 89  
 
 90  
         public void end(ReadContext context) throws Exception {
 91  
             // do nothing
 92  52
         }
 93  
         
 94  
     }
 95  
 
 96  
     /**
 97  
      * Basic action.
 98  
      * 
 99  
      * @author <a href='http://jakarta.apache.org/'>Apache Commons Team</a>
 100  
      * @version $Revision: 397646 $
 101  
      */
 102  1624
     public static class Base extends MappingAction {
 103  
         
 104  
         public MappingAction next(
 105  
             String namespace,
 106  
             String name,
 107  
             Attributes attributes,
 108  
             ReadContext context)
 109  
             throws Exception {       
 110  
         
 111  7786
             return context.getActionMappingStrategy().getMappingAction(namespace, name, attributes, context);
 112  
         }
 113  
         
 114  
         /**
 115  
          * @see org.apache.commons.betwixt.io.read.MappingAction#begin(String, String, Attributes, ReadContext)
 116  
          */
 117  
         public MappingAction begin(
 118  
             String namespace,
 119  
             String name,
 120  
             Attributes attributes,
 121  
             ReadContext context)
 122  
             throws Exception {
 123  
             // TODO: i'm not too sure about this part of the design
 124  
             // i'm not sure whether base should give base behaviour or if it should give standard behaviour
 125  
             // i'm hoping that things will become clearer once the descriptor logic has been cleared 
 126  4935
             ElementDescriptor descriptor = context.getCurrentDescriptor();
 127  4935
             if (descriptor != null) {
 128  
 
 129  4584
                 AttributeDescriptor[] attributeDescriptors =
 130  4584
                     descriptor.getAttributeDescriptors();
 131  4584
                 context.populateAttributes(attributeDescriptors, attributes);
 132  
             }
 133  4935
             return this;
 134  
         }
 135  
 
 136  
         /**
 137  
          * @see MappingAction#body(String, ReadContext)
 138  
          */
 139  
         public void body(String text, ReadContext context) throws Exception {
 140  
             // do nothing
 141  1591
         }
 142  
 
 143  
         /**
 144  
          * @see MappingAction#end(ReadContext)
 145  
          */
 146  
         public void end(ReadContext context) throws Exception {
 147  
             // do nothing
 148  
             // TODO: this is a temporary refactoring
 149  
             // it would be better to do this in the rule
 150  
             // need to move more logic into the context and out of the rule
 151  5291
             context.popElement();
 152  5291
         }
 153  
 
 154  
     }
 155  
 }