You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventMethodModel.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /* $Id$ */
  18. package org.apache.fop.events.model;
  19. import java.io.Serializable;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import org.xml.sax.ContentHandler;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.helpers.AttributesImpl;
  26. import org.apache.xmlgraphics.util.XMLizable;
  27. /**
  28. * Represents an event method. Each method in an event producer interface will result in one
  29. * instance of <code>EventMethodModel</code>.
  30. */
  31. public class EventMethodModel implements Serializable, XMLizable {
  32. private static final long serialVersionUID = -7548882973341444354L;
  33. private String methodName;
  34. private EventSeverity severity;
  35. private List params = new java.util.ArrayList();
  36. private String exceptionClass;
  37. /**
  38. * Creates an new instance.
  39. * @param methodName the event method's name
  40. * @param severity the event severity
  41. */
  42. public EventMethodModel(String methodName, EventSeverity severity) {
  43. this.methodName = methodName;
  44. this.severity = severity;
  45. }
  46. /**
  47. * Adds a method parameter.
  48. * @param param the method parameter
  49. */
  50. public void addParameter(Parameter param) {
  51. this.params.add(param);
  52. }
  53. /**
  54. * Adds a method parameter.
  55. * @param type the type of the parameter
  56. * @param name the name of the parameter
  57. * @return the resulting Parameter instance
  58. */
  59. public Parameter addParameter(Class type, String name) {
  60. Parameter param = new Parameter(type, name);
  61. addParameter(param);
  62. return param;
  63. }
  64. /**
  65. * Sets the event method name.
  66. * @param name the event name
  67. */
  68. public void setMethodName(String name) {
  69. this.methodName = name;
  70. }
  71. /**
  72. * Returns the event method name
  73. * @return the event name
  74. */
  75. public String getMethodName() {
  76. return this.methodName;
  77. }
  78. /**
  79. * Sets the event's severity level.
  80. * @param severity the severity
  81. */
  82. public void setSeverity(EventSeverity severity) {
  83. this.severity = severity;
  84. }
  85. /**
  86. * Returns the event's severity level.
  87. * @return the severity
  88. */
  89. public EventSeverity getSeverity() {
  90. return this.severity;
  91. }
  92. /**
  93. * Returns an unmodifiable list of parameters for this event method.
  94. * @return the list of parameters
  95. */
  96. public List getParameters() {
  97. return Collections.unmodifiableList(this.params);
  98. }
  99. /**
  100. * Sets the primary exception class for this event method. Note: Not all event methods throw
  101. * exceptions!
  102. * @param exceptionClass the exception class
  103. */
  104. public void setExceptionClass(String exceptionClass) {
  105. this.exceptionClass = exceptionClass;
  106. }
  107. /**
  108. * Returns the primary exception class for this event method. This method returns null if
  109. * the event is only informational or just a warning.
  110. * @return the primary exception class or null
  111. */
  112. public String getExceptionClass() {
  113. return this.exceptionClass;
  114. }
  115. /** {@inheritDoc} */
  116. public void toSAX(ContentHandler handler) throws SAXException {
  117. AttributesImpl atts = new AttributesImpl();
  118. atts.addAttribute(null, "name", "name", "CDATA", getMethodName());
  119. atts.addAttribute(null, "severity", "severity", "CDATA", getSeverity().getName());
  120. if (getExceptionClass() != null) {
  121. atts.addAttribute(null, "exception", "exception", "CDATA", getExceptionClass());
  122. }
  123. String elName = "method";
  124. handler.startElement(null, elName, elName, atts);
  125. Iterator iter = this.params.iterator();
  126. while (iter.hasNext()) {
  127. ((XMLizable)iter.next()).toSAX(handler);
  128. }
  129. handler.endElement(null, elName, elName);
  130. }
  131. /**
  132. * Represents an event parameter.
  133. */
  134. public static class Parameter implements Serializable, XMLizable {
  135. private static final long serialVersionUID = 6062500277953887099L;
  136. private Class type;
  137. private String name;
  138. /**
  139. * Creates a new event parameter.
  140. * @param type the parameter type
  141. * @param name the parameter name
  142. */
  143. public Parameter(Class type, String name) {
  144. this.type = type;
  145. this.name = name;
  146. }
  147. /**
  148. * Returns the parameter type.
  149. * @return the parameter type
  150. */
  151. public Class getType() {
  152. return this.type;
  153. }
  154. /**
  155. * Returns the parameter name.
  156. * @return the parameter name
  157. */
  158. public String getName() {
  159. return this.name;
  160. }
  161. /** {@inheritDoc} */
  162. public void toSAX(ContentHandler handler) throws SAXException {
  163. AttributesImpl atts = new AttributesImpl();
  164. atts.addAttribute(null, "type", "type", "CDATA", getType().getName());
  165. atts.addAttribute(null, "name", "name", "CDATA", getName());
  166. String elName = "parameter";
  167. handler.startElement(null, elName, elName, atts);
  168. handler.endElement(null, elName, elName);
  169. }
  170. }
  171. }