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