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

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