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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public 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. public EventProducerCollector() {
  57. this.tagFactory = createDocletTagFactory();
  58. }
  59. protected DocletTagFactory createDocletTagFactory() {
  60. return new DefaultDocletTagFactory();
  61. }
  62. public void scanFile(File src, String filename)
  63. throws IOException, EventConventionException, ClassNotFoundException {
  64. JavaDocBuilder builder = new JavaDocBuilder(this.tagFactory);
  65. builder.addSource(src);
  66. JavaClass[] classes = builder.getClasses();
  67. for (int i = 0, c = classes.length; i < c; i++) {
  68. JavaClass clazz = classes[i];
  69. if (clazz.isInterface() && implementsInterface(clazz, CLASSNAME_EVENT_PRODUCER)) {
  70. processEventProducerInterface(clazz, filename);
  71. }
  72. }
  73. }
  74. private boolean implementsInterface(JavaClass clazz, String intf) {
  75. JavaClass[] classes = clazz.getImplementedInterfaces();
  76. for (int i = 0, c = classes.length; i < c; i++) {
  77. JavaClass cl = classes[i];
  78. if (cl.getFullyQualifiedName().equals(intf)) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. /**
  85. * Processes an EventProducer interface and creates an EventProducerModel from it.
  86. * @param clazz the EventProducer interface
  87. * @param javaFilename the filename of the Java source of the interface
  88. * @throws EventConventionException if the event producer conventions are violated
  89. * @throws ClassNotFoundException if a required class cannot be found
  90. */
  91. protected void processEventProducerInterface(JavaClass clazz, String javaFilename)
  92. throws EventConventionException, ClassNotFoundException {
  93. EventProducerModel prodMeta = new EventProducerModel(clazz.getFullyQualifiedName());
  94. JavaMethod[] methods = clazz.getMethods(true);
  95. for (int i = 0, c = methods.length; i < c; i++) {
  96. JavaMethod method = methods[i];
  97. EventMethodModel methodMeta = createMethodModel(method);
  98. prodMeta.addMethod(methodMeta);
  99. }
  100. this.model.addProducer(prodMeta);
  101. }
  102. private EventMethodModel createMethodModel(JavaMethod method)
  103. throws EventConventionException, ClassNotFoundException {
  104. JavaClass clazz = method.getParentClass();
  105. //Check EventProducer conventions
  106. if (!method.getReturns().isVoid()) {
  107. throw new EventConventionException("All methods of interface "
  108. + clazz.getFullyQualifiedName() + " must have return type 'void'!");
  109. }
  110. String methodSig = clazz.getFullyQualifiedName() + "." + method.getCallSignature();
  111. JavaParameter[] params = method.getParameters();
  112. if (params.length < 1) {
  113. throw new EventConventionException("The method " + methodSig
  114. + " must have at least one parameter: 'Object source'!");
  115. }
  116. Type firstType = params[0].getType();
  117. if (firstType.isPrimitive() || !"source".equals(params[0].getName())) {
  118. throw new EventConventionException("The first parameter of the method " + methodSig
  119. + " must be: 'Object source'!");
  120. }
  121. //build method model
  122. DocletTag tag = method.getTagByName("event.severity");
  123. EventSeverity severity;
  124. if (tag != null) {
  125. severity = EventSeverity.valueOf(tag.getValue());
  126. } else {
  127. severity = EventSeverity.INFO;
  128. }
  129. EventMethodModel methodMeta = new EventMethodModel(
  130. method.getName(), severity);
  131. if (params.length > 1) {
  132. for (int j = 1, cj = params.length; j < cj; j++) {
  133. JavaParameter p = params[j];
  134. Class type;
  135. JavaClass pClass = p.getType().getJavaClass();
  136. if (p.getType().isPrimitive()) {
  137. type = (Class)PRIMITIVE_MAP.get(pClass.getName());
  138. if (type == null) {
  139. throw new UnsupportedOperationException(
  140. "Primitive datatype not supported: " + pClass.getName());
  141. }
  142. } else {
  143. String className = pClass.getFullyQualifiedName();
  144. type = Class.forName(className);
  145. }
  146. methodMeta.addParameter(type, p.getName());
  147. }
  148. }
  149. Type[] exceptions = method.getExceptions();
  150. if (exceptions != null && exceptions.length > 0) {
  151. //We only use the first declared exception because that is always thrown
  152. JavaClass cl = exceptions[0].getJavaClass();
  153. methodMeta.setExceptionClass(cl.getFullyQualifiedName());
  154. methodMeta.setSeverity(EventSeverity.FATAL); //In case it's not set in the comments
  155. }
  156. return methodMeta;
  157. }
  158. public EventModel getModel() {
  159. return this.model;
  160. }
  161. public void saveModelToXML(File modelFile) throws IOException {
  162. getModel().saveToXML(modelFile);
  163. }
  164. }