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.

PageProductionException.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.fo.pagination;
  19. import java.util.Locale;
  20. import org.xml.sax.Locator;
  21. import org.xml.sax.helpers.LocatorImpl;
  22. import org.apache.fop.events.Event;
  23. import org.apache.fop.events.EventExceptionManager.ExceptionFactory;
  24. import org.apache.fop.events.EventFormatter;
  25. /**
  26. * Exception thrown by FOP if there is a problem while producing new pages.
  27. */
  28. public class PageProductionException extends RuntimeException {
  29. private static final long serialVersionUID = -5126033718398975158L;
  30. private String localizedMessage;
  31. private transient Locator locator;
  32. /**
  33. * Creates a new PageProductionException.
  34. * @param message the message
  35. */
  36. public PageProductionException(String message) {
  37. super(message);
  38. }
  39. /**
  40. * Creates a new PageProductionException.
  41. * @param message the message
  42. * @param locator the optional locator that points to the error in the source file
  43. */
  44. public PageProductionException(String message, Locator locator) {
  45. super(message);
  46. setLocator(locator);
  47. }
  48. /**
  49. * Set a location associated with the exception.
  50. * @param locator the locator holding the location.
  51. */
  52. public void setLocator(Locator locator) {
  53. this.locator = locator != null ? new LocatorImpl(locator) : null;
  54. }
  55. /**
  56. * Returns the locattion associated with the exception.
  57. * @return the locator or null if the location information is not available
  58. */
  59. public Locator getLocator() {
  60. return this.locator;
  61. }
  62. /**
  63. * Sets the localized message for this exception.
  64. * @param msg the localized message
  65. */
  66. public void setLocalizedMessage(String msg) {
  67. this.localizedMessage = msg;
  68. }
  69. /** {@inheritDoc} */
  70. public String getLocalizedMessage() {
  71. if (this.localizedMessage != null) {
  72. return this.localizedMessage;
  73. } else {
  74. return super.getLocalizedMessage();
  75. }
  76. }
  77. /** Exception factory for {@link PageProductionException}. */
  78. public static class PageProductionExceptionFactory implements ExceptionFactory {
  79. /** {@inheritDoc} */
  80. public Throwable createException(Event event) {
  81. Object obj = event.getParam("loc");
  82. Locator loc = (obj instanceof Locator ? (Locator)obj : null);
  83. String msg = EventFormatter.format(event, Locale.ENGLISH);
  84. PageProductionException ex = new PageProductionException(msg, loc);
  85. if (!Locale.ENGLISH.equals(Locale.getDefault())) {
  86. ex.setLocalizedMessage(EventFormatter.format(event));
  87. }
  88. return ex;
  89. }
  90. /** {@inheritDoc} */
  91. public Class<PageProductionException> getExceptionClass() {
  92. return PageProductionException.class;
  93. }
  94. }
  95. }