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.

EventModelParser.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.util.Stack;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.sax.SAXResult;
  24. import javax.xml.transform.sax.SAXTransformerFactory;
  25. import org.xml.sax.Attributes;
  26. import org.xml.sax.ContentHandler;
  27. import org.xml.sax.SAXException;
  28. import org.xml.sax.helpers.DefaultHandler;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.apache.fop.util.DefaultErrorListener;
  32. /**
  33. * This is a parser for the event model XML.
  34. */
  35. public final class EventModelParser {
  36. private EventModelParser() {
  37. }
  38. /** Logger instance */
  39. private static final Log log // CSOK: ConstantName
  40. = LogFactory.getLog(EventModelParser.class);
  41. private static SAXTransformerFactory tFactory
  42. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  43. /**
  44. * Parses an event model file into an EventModel instance.
  45. * @param src the Source instance pointing to the XML file
  46. * @return the created event model structure
  47. * @throws TransformerException if an error occurs while parsing the XML file
  48. */
  49. public static EventModel parse(Source src)
  50. throws TransformerException {
  51. Transformer transformer = tFactory.newTransformer();
  52. transformer.setErrorListener(new DefaultErrorListener(log));
  53. EventModel model = new EventModel();
  54. SAXResult res = new SAXResult(getContentHandler(model));
  55. transformer.transform(src, res);
  56. return model;
  57. }
  58. /**
  59. * Creates a new ContentHandler instance that you can send the event model XML to. The parsed
  60. * content is accumulated in the model structure.
  61. * @param model the EventModel
  62. * @return the ContentHandler instance to receive the SAX stream from the XML file
  63. */
  64. public static ContentHandler getContentHandler(EventModel model) {
  65. return new Handler(model);
  66. }
  67. private static class Handler extends DefaultHandler {
  68. private EventModel model;
  69. private Stack objectStack = new Stack();
  70. public Handler(EventModel model) {
  71. this.model = model;
  72. }
  73. /** {@inheritDoc} */
  74. public void startElement(String uri, String localName, String qName, Attributes attributes)
  75. throws SAXException {
  76. try {
  77. if ("event-model".equals(localName)) {
  78. if (objectStack.size() > 0) {
  79. throw new SAXException("event-model must be the root element");
  80. }
  81. objectStack.push(model);
  82. } else if ("producer".equals(localName)) {
  83. EventProducerModel producer = new EventProducerModel(
  84. attributes.getValue("name"));
  85. EventModel parent = (EventModel)objectStack.peek();
  86. parent.addProducer(producer);
  87. objectStack.push(producer);
  88. } else if ("method".equals(localName)) {
  89. EventSeverity severity = EventSeverity.valueOf(attributes.getValue("severity"));
  90. String ex = attributes.getValue("exception");
  91. EventMethodModel method = new EventMethodModel(
  92. attributes.getValue("name"), severity);
  93. if (ex != null && ex.length() > 0) {
  94. method.setExceptionClass(ex);
  95. }
  96. EventProducerModel parent = (EventProducerModel)objectStack.peek();
  97. parent.addMethod(method);
  98. objectStack.push(method);
  99. } else if ("parameter".equals(localName)) {
  100. String className = attributes.getValue("type");
  101. Class type;
  102. try {
  103. type = Class.forName(className);
  104. } catch (ClassNotFoundException e) {
  105. throw new SAXException("Could not find Class for: " + className, e);
  106. }
  107. String name = attributes.getValue("name");
  108. EventMethodModel parent = (EventMethodModel)objectStack.peek();
  109. objectStack.push(parent.addParameter(type, name));
  110. } else {
  111. throw new SAXException("Invalid element: " + qName);
  112. }
  113. } catch (ClassCastException cce) {
  114. throw new SAXException("XML format error: " + qName, cce);
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. public void endElement(String uri, String localName, String qName) throws SAXException {
  119. objectStack.pop();
  120. }
  121. }
  122. }