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.

EventProducerCollector.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.tools;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.fop.events.EventProducer;
  26. import org.apache.fop.events.model.EventMethodModel;
  27. import org.apache.fop.events.model.EventModel;
  28. import org.apache.fop.events.model.EventProducerModel;
  29. import org.apache.fop.events.model.EventSeverity;
  30. import com.thoughtworks.qdox.JavaDocBuilder;
  31. import com.thoughtworks.qdox.model.DefaultDocletTagFactory;
  32. import com.thoughtworks.qdox.model.DocletTag;
  33. import com.thoughtworks.qdox.model.DocletTagFactory;
  34. import com.thoughtworks.qdox.model.JavaClass;
  35. import com.thoughtworks.qdox.model.JavaMethod;
  36. import com.thoughtworks.qdox.model.JavaParameter;
  37. import com.thoughtworks.qdox.model.Type;
  38. /**
  39. * Finds EventProducer interfaces and builds the event model for them.
  40. */
  41. class EventProducerCollector {
  42. private static final String CLASSNAME_EVENT_PRODUCER = EventProducer.class.getName();
  43. private static final Map PRIMITIVE_MAP;
  44. static {
  45. Map m = new java.util.HashMap();
  46. m.put("boolean", Boolean.class);
  47. m.put("byte", Byte.class);
  48. m.put("char", Character.class);
  49. m.put("short", Short.class);
  50. m.put("int", Integer.class);
  51. m.put("long", Long.class);
  52. m.put("float", Float.class);
  53. m.put("double", Double.class);
  54. PRIMITIVE_MAP = Collections.unmodifiableMap(m);
  55. }
  56. private DocletTagFactory tagFactory;
  57. private List models = new ArrayList();
  58. /**
  59. * Creates a new EventProducerCollector.
  60. */
  61. EventProducerCollector() {
  62. this.tagFactory = createDocletTagFactory();
  63. }
  64. /**
  65. * Creates the {@link DocletTagFactory} to be used by the collector.
  66. * @return the doclet tag factory
  67. */
  68. protected DocletTagFactory createDocletTagFactory() {
  69. return new DefaultDocletTagFactory();
  70. }
  71. /**
  72. * Scans a file and processes it if it extends the {@link EventProducer} interface.
  73. * @param src the source file (a Java source file)
  74. * @return true if the file contained an EventProducer interface
  75. * @throws IOException if an I/O error occurs
  76. * @throws EventConventionException if the EventProducer conventions are violated
  77. * @throws ClassNotFoundException if a required class cannot be found
  78. */
  79. public boolean scanFile(File src)
  80. throws IOException, EventConventionException, ClassNotFoundException {
  81. JavaDocBuilder builder = new JavaDocBuilder(this.tagFactory);
  82. builder.addSource(src);
  83. JavaClass[] classes = builder.getClasses();
  84. boolean eventProducerFound = false;
  85. for (int i = 0, c = classes.length; i < c; i++) {
  86. JavaClass clazz = classes[i];
  87. if (clazz.isInterface() && implementsInterface(clazz, CLASSNAME_EVENT_PRODUCER)) {
  88. processEventProducerInterface(clazz);
  89. eventProducerFound = true;
  90. }
  91. }
  92. return eventProducerFound;
  93. }
  94. private boolean implementsInterface(JavaClass clazz, String intf) {
  95. JavaClass[] classes = clazz.getImplementedInterfaces();
  96. for (int i = 0, c = classes.length; i < c; i++) {
  97. JavaClass cl = classes[i];
  98. if (cl.getFullyQualifiedName().equals(intf)) {
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. /**
  105. * Processes an EventProducer interface and creates an EventProducerModel from it.
  106. * @param clazz the EventProducer interface
  107. * @throws EventConventionException if the event producer conventions are violated
  108. * @throws ClassNotFoundException if a required class cannot be found
  109. */
  110. protected void processEventProducerInterface(JavaClass clazz)
  111. throws EventConventionException, ClassNotFoundException {
  112. EventProducerModel prodMeta = new EventProducerModel(clazz.getFullyQualifiedName());
  113. JavaMethod[] methods = clazz.getMethods(true);
  114. for (int i = 0, c = methods.length; i < c; i++) {
  115. JavaMethod method = methods[i];
  116. EventMethodModel methodMeta = createMethodModel(method);
  117. prodMeta.addMethod(methodMeta);
  118. }
  119. EventModel model = new EventModel();
  120. model.addProducer(prodMeta);
  121. models.add(model);
  122. }
  123. private EventMethodModel createMethodModel(JavaMethod method)
  124. throws EventConventionException, ClassNotFoundException {
  125. JavaClass clazz = method.getParentClass();
  126. //Check EventProducer conventions
  127. if (!method.getReturns().isVoid()) {
  128. throw new EventConventionException("All methods of interface "
  129. + clazz.getFullyQualifiedName() + " must have return type 'void'!");
  130. }
  131. String methodSig = clazz.getFullyQualifiedName() + "." + method.getCallSignature();
  132. JavaParameter[] params = method.getParameters();
  133. if (params.length < 1) {
  134. throw new EventConventionException("The method " + methodSig
  135. + " must have at least one parameter: 'Object source'!");
  136. }
  137. Type firstType = params[0].getType();
  138. if (firstType.isPrimitive() || !"source".equals(params[0].getName())) {
  139. throw new EventConventionException("The first parameter of the method " + methodSig
  140. + " must be: 'Object source'!");
  141. }
  142. //build method model
  143. DocletTag tag = method.getTagByName("event.severity");
  144. EventSeverity severity;
  145. if (tag != null) {
  146. severity = EventSeverity.valueOf(tag.getValue());
  147. } else {
  148. severity = EventSeverity.INFO;
  149. }
  150. EventMethodModel methodMeta = new EventMethodModel(
  151. method.getName(), severity);
  152. if (params.length > 1) {
  153. for (int j = 1, cj = params.length; j < cj; j++) {
  154. JavaParameter p = params[j];
  155. Class type;
  156. JavaClass pClass = p.getType().getJavaClass();
  157. if (p.getType().isPrimitive()) {
  158. type = (Class)PRIMITIVE_MAP.get(pClass.getName());
  159. if (type == null) {
  160. throw new UnsupportedOperationException(
  161. "Primitive datatype not supported: " + pClass.getName());
  162. }
  163. } else {
  164. String className = pClass.getFullyQualifiedName();
  165. type = Class.forName(className);
  166. }
  167. methodMeta.addParameter(type, p.getName());
  168. }
  169. }
  170. Type[] exceptions = method.getExceptions();
  171. if (exceptions != null && exceptions.length > 0) {
  172. //We only use the first declared exception because that is always thrown
  173. JavaClass cl = exceptions[0].getJavaClass();
  174. methodMeta.setExceptionClass(cl.getFullyQualifiedName());
  175. methodMeta.setSeverity(EventSeverity.FATAL); //In case it's not set in the comments
  176. }
  177. return methodMeta;
  178. }
  179. /**
  180. * Returns the event model that has been accumulated.
  181. * @return the event model.
  182. */
  183. public List getModels() {
  184. return this.models;
  185. }
  186. }