View Javadoc

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/'>Jakarta Commons Team</a>
27   * @version $Revision: 155402 $
28   */
29  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      public static final MappingAction EMPTY = new MappingAction.Base();
73  
74      public static final MappingAction IGNORE = new MappingAction.Ignore();    
75      
76      private static final class Ignore extends MappingAction {
77  
78          public MappingAction next(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
79              return this;
80          }
81  
82          public MappingAction begin(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
83              return this;
84          }
85  
86          public void body(String text, ReadContext context) throws Exception {
87              // do nothing
88          }
89  
90          public void end(ReadContext context) throws Exception {
91              // do nothing
92          }
93          
94      }
95  
96      /***
97       * Basic action.
98       * 
99       * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
100      * @version $Revision: 155402 $
101      */
102     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             return context.getActionMappingStrategy().getMappingAction(namespace, name, attributes, context);
112         }
113         
114         /* (non-Javadoc)
115          * @see org.apache.commons.betwixt.io.read.MappingAction#begin(java.lang.String, java.lang.String, org.xml.sax.Attributes, org.apache.commons.betwixt.io.read.ReadContext, org.apache.commons.betwixt.XMLIntrospector)
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             ElementDescriptor descriptor = context.getCurrentDescriptor();
127             if (descriptor != null) {
128 
129                 AttributeDescriptor[] attributeDescriptors =
130                     descriptor.getAttributeDescriptors();
131                 context.populateAttributes(attributeDescriptors, attributes);
132             }
133             return this;
134         }
135 
136         /* (non-Javadoc)
137          * @see org.apache.commons.betwixt.io.read.MappingAction#body(java.lang.String, org.apache.commons.betwixt.io.read.ReadContext, org.apache.commons.betwixt.XMLIntrospector)
138          */
139         public void body(String text, ReadContext context) throws Exception {
140             // do nothing
141         }
142 
143         /* (non-Javadoc)
144          * @see org.apache.commons.betwixt.io.read.MappingAction#end(org.apache.commons.betwixt.io.read.ReadContext, org.apache.commons.digester.Digester, org.apache.commons.betwixt.XMLIntrospector)
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             context.popElement();
152         }
153 
154     }
155 }