001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt.io.read;
018:
019: import org.apache.commons.betwixt.AttributeDescriptor;
020: import org.apache.commons.betwixt.ElementDescriptor;
021: import org.xml.sax.Attributes;
022:
023: /**
024: * Executes mapping action for a subgraph.
025: * It is intended that most MappingAction's will not need to maintain state.
026: *
027: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
028: * @version $Revision: 438373 $
029: */
030: public abstract class MappingAction {
031:
032: public abstract MappingAction next(String namespace, String name,
033: Attributes attributes, ReadContext context)
034: throws Exception;
035:
036: /**
037: * Executes mapping action on new element.
038: * @param namespace
039: * @param name
040: * @param attributes Attributes not null
041: * @param context Context not null
042: * @return the MappingAction to be used to map the sub-graph
043: * under this element
044: * @throws Exception
045: */
046: public abstract MappingAction begin(String namespace, String name,
047: Attributes attributes, ReadContext context)
048: throws Exception;
049:
050: /**
051: * Executes mapping action for element body text
052: * @param text
053: * @param context
054: * @throws Exception
055: */
056: public abstract void body(String text, ReadContext context)
057: throws Exception;
058:
059: /**
060: * Executes mapping action one element ends
061: * @param context
062: * @throws Exception
063: */
064: public abstract void end(ReadContext context) throws Exception;
065:
066: public static final MappingAction EMPTY = new MappingAction.Base();
067:
068: public static final MappingAction IGNORE = new MappingAction.Ignore();
069:
070: private static final class Ignore extends MappingAction {
071:
072: public MappingAction next(String namespace, String name,
073: Attributes attributes, ReadContext context)
074: throws Exception {
075: return this ;
076: }
077:
078: public MappingAction begin(String namespace, String name,
079: Attributes attributes, ReadContext context)
080: throws Exception {
081: return this ;
082: }
083:
084: public void body(String text, ReadContext context)
085: throws Exception {
086: // do nothing
087: }
088:
089: public void end(ReadContext context) throws Exception {
090: // do nothing
091: }
092:
093: }
094:
095: /**
096: * Basic action.
097: *
098: * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
099: * @version $Revision: 438373 $
100: */
101: public static class Base extends MappingAction {
102:
103: public MappingAction next(String namespace, String name,
104: Attributes attributes, ReadContext context)
105: throws Exception {
106:
107: return context.getActionMappingStrategy().getMappingAction(
108: namespace, name, attributes, context);
109: }
110:
111: /**
112: * @see org.apache.commons.betwixt.io.read.MappingAction#begin(String, String, Attributes, ReadContext)
113: */
114: public MappingAction begin(String namespace, String name,
115: Attributes attributes, ReadContext context)
116: throws Exception {
117: // TODO: i'm not too sure about this part of the design
118: // i'm not sure whether base should give base behaviour or if it should give standard behaviour
119: // i'm hoping that things will become clearer once the descriptor logic has been cleared
120: ElementDescriptor descriptor = context
121: .getCurrentDescriptor();
122: if (descriptor != null) {
123:
124: AttributeDescriptor[] attributeDescriptors = descriptor
125: .getAttributeDescriptors();
126: context.populateAttributes(attributeDescriptors,
127: attributes);
128: }
129: return this ;
130: }
131:
132: /**
133: * @see MappingAction#body(String, ReadContext)
134: */
135: public void body(String text, ReadContext context)
136: throws Exception {
137: // do nothing
138: }
139:
140: /**
141: * @see MappingAction#end(ReadContext)
142: */
143: public void end(ReadContext context) throws Exception {
144: // do nothing
145: // TODO: this is a temporary refactoring
146: // it would be better to do this in the rule
147: // need to move more logic into the context and out of the rule
148: context.popElement();
149: }
150:
151: }
152: }
|