Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

EventProducerCollector.java 8.1KB

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