View Javadoc

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