Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractEventModelFactory.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.io.InputStream;
  20. import java.util.MissingResourceException;
  21. import javax.xml.transform.TransformerException;
  22. import javax.xml.transform.stream.StreamSource;
  23. import org.apache.commons.io.IOUtils;
  24. import org.apache.fop.events.DefaultEventBroadcaster;
  25. /**
  26. * This interface is used to instantiate (load, parse) event models.
  27. */
  28. public abstract class AbstractEventModelFactory implements EventModelFactory {
  29. /**
  30. * Loads an event model and returns its instance.
  31. * @param resourceBaseClass base class to use for loading resources
  32. * @param resourceName the resource name pointing to the event model to be loaded
  33. * @return the newly loaded event model.
  34. */
  35. public EventModel loadModel(Class resourceBaseClass, String resourceName) {
  36. InputStream in = resourceBaseClass.getResourceAsStream(resourceName);
  37. if (in == null) {
  38. throw new MissingResourceException(
  39. "File " + resourceName + " not found",
  40. DefaultEventBroadcaster.class.getName(), "");
  41. }
  42. try {
  43. return EventModelParser.parse(new StreamSource(in));
  44. } catch (TransformerException e) {
  45. throw new MissingResourceException(
  46. "Error reading " + resourceName + ": " + e.getMessage(),
  47. DefaultEventBroadcaster.class.getName(), "");
  48. } finally {
  49. IOUtils.closeQuietly(in);
  50. }
  51. }
  52. }