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. import org.apache.commons.betwixt.AttributeDescriptor;
  18. import org.apache.commons.betwixt.ElementDescriptor;
  19. import org.xml.sax.Attributes;
  20. /**
  21. * Executes mapping action for a subgraph.
  22. * It is intended that most MappingAction's will not need to maintain state.
  23. *
  24. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  25. * @version $Revision: 1.2 $
  26. */
  27. public abstract class MappingAction {
  28. public abstract MappingAction next(
  29. String namespace,
  30. String name,
  31. Attributes attributes,
  32. ReadContext context)
  33. throws Exception;
  34. /**
  35. * Executes mapping action on new element.
  36. * @param namespace
  37. * @param name
  38. * @param attributes Attributes not null
  39. * @param context Context not null
  40. * @return the MappingAction to be used to map the sub-graph
  41. * under this element
  42. * @throws Exception
  43. */
  44. public abstract MappingAction begin(
  45. String namespace,
  46. String name,
  47. Attributes attributes,
  48. ReadContext context)
  49. throws Exception;
  50. /**
  51. * Executes mapping action for element body text
  52. * @param text
  53. * @param context
  54. * @throws Exception
  55. */
  56. public abstract void body(String text, ReadContext context)
  57. throws Exception;
  58. /**
  59. * Executes mapping action one element ends
  60. * @param context
  61. * @throws Exception
  62. */
  63. public abstract void end(ReadContext context) throws Exception;
  64. public static final MappingAction EMPTY = new MappingAction.Base();
  65. public static final MappingAction IGNORE = new MappingAction.Ignore();
  66. private static final class Ignore extends MappingAction {
  67. public MappingAction next(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
  68. return this;
  69. }
  70. public MappingAction begin(String namespace, String name, Attributes attributes, ReadContext context) throws Exception {
  71. return this;
  72. }
  73. public void body(String text, ReadContext context) throws Exception {
  74. // do nothing
  75. }
  76. public void end(ReadContext context) throws Exception {
  77. // do nothing
  78. }
  79. }
  80. /**
  81. * Basic action.
  82. *
  83. * @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
  84. * @version $Revision: 1.2 $
  85. */
  86. public static class Base extends MappingAction {
  87. public MappingAction next(
  88. String namespace,
  89. String name,
  90. Attributes attributes,
  91. ReadContext context)
  92. throws Exception {
  93. return context.getActionMappingStrategy().getMappingAction(namespace, name, attributes, context);
  94. }
  95. /* (non-Javadoc)
  96. * @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)
  97. */
  98. public MappingAction begin(
  99. String namespace,
  100. String name,
  101. Attributes attributes,
  102. ReadContext context)
  103. throws Exception {
  104. // TODO: i'm not too sure about this part of the design
  105. // i'm not sure whether base should give base behaviour or if it should give standard behaviour
  106. // i'm hoping that things will become clearer once the descriptor logic has been cleared
  107. ElementDescriptor descriptor = context.getCurrentDescriptor();
  108. if (descriptor != null) {
  109. AttributeDescriptor[] attributeDescriptors =
  110. descriptor.getAttributeDescriptors();
  111. context.populateAttributes(attributeDescriptors, attributes);
  112. }
  113. return this;
  114. }
  115. /* (non-Javadoc)
  116. * @see org.apache.commons.betwixt.io.read.MappingAction#body(java.lang.String, org.apache.commons.betwixt.io.read.ReadContext, org.apache.commons.betwixt.XMLIntrospector)
  117. */
  118. public void body(String text, ReadContext context) throws Exception {
  119. // do nothing
  120. }
  121. /* (non-Javadoc)
  122. * @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)
  123. */
  124. public void end(ReadContext context) throws Exception {
  125. // do nothing
  126. // TODO: this is a temporary refactoring
  127. // it would be better to do this in the rule
  128. // need to move more logic into the context and out of the rule
  129. context.popElement();
  130. }
  131. }
  132. }