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.

LayoutException.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.layoutmgr;
  19. import java.util.Locale;
  20. import org.apache.fop.events.Event;
  21. import org.apache.fop.events.EventExceptionManager.ExceptionFactory;
  22. import org.apache.fop.events.EventFormatter;
  23. /**
  24. * Exception thrown by FOP if an unrecoverable layout error occurs. An example: An area overflows
  25. * a viewport that has overflow="error-if-overflow".
  26. *
  27. * TODO Discuss if this should become a checked exception.
  28. */
  29. public class LayoutException extends RuntimeException {
  30. private static final long serialVersionUID = 5157080040923740433L;
  31. private String localizedMessage;
  32. private LayoutManager layoutManager;
  33. /**
  34. * Constructs a new layout exception with the specified detail message.
  35. * @param message the detail message.
  36. */
  37. public LayoutException(String message) {
  38. this(message, null);
  39. }
  40. /**
  41. * Constructs a new layout exception with the specified detail message.
  42. * @param message the detail message
  43. * @param lm the layout manager that throws the exception
  44. */
  45. public LayoutException(String message, LayoutManager lm) {
  46. super(message);
  47. this.layoutManager = lm;
  48. }
  49. /**
  50. * Sets the localized message for this exception.
  51. * @param msg the localized message
  52. */
  53. public void setLocalizedMessage(String msg) {
  54. this.localizedMessage = msg;
  55. }
  56. /** {@inheritDoc} */
  57. public String getLocalizedMessage() {
  58. if (this.localizedMessage != null) {
  59. return this.localizedMessage;
  60. } else {
  61. return super.getLocalizedMessage();
  62. }
  63. }
  64. /**
  65. * Returns the layout manager that detected the problem.
  66. * @return the layout manager (or null)
  67. */
  68. public LayoutManager getLayoutManager() {
  69. return this.layoutManager;
  70. }
  71. /** Exception factory for {@link LayoutException}. */
  72. public static class LayoutExceptionFactory implements ExceptionFactory {
  73. /** {@inheritDoc} */
  74. public Throwable createException(Event event) {
  75. Object source = event.getSource();
  76. LayoutManager lm = (source instanceof LayoutManager) ? (LayoutManager)source : null;
  77. String msg = EventFormatter.format(event, Locale.ENGLISH);
  78. LayoutException ex = new LayoutException(msg, lm);
  79. if (!Locale.ENGLISH.equals(Locale.getDefault())) {
  80. ex.setLocalizedMessage(EventFormatter.format(event));
  81. }
  82. return ex;
  83. }
  84. /** {@inheritDoc} */
  85. public Class<LayoutException> getExceptionClass() {
  86. return LayoutException.class;
  87. }
  88. }
  89. }