1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: MsgMgr.java 468645 2006-10-28 06:57:24Z minchau $
20 */
21package org.apache.xalan.transformer;
22
23import javax.xml.transform.ErrorListener;
24import javax.xml.transform.SourceLocator;
25import javax.xml.transform.TransformerException;
26
27import org.apache.xalan.res.XSLMessages;
28
29import org.w3c.dom.Node;
30
31/**
32 * This class will manage error messages, warning messages, and other types of
33 * message events.
34 */
35public class MsgMgr
36{
37
38  /**
39   * Create a message manager object.
40   *
41   * @param transformer non transformer instance
42   */
43  public MsgMgr(TransformerImpl transformer)
44  {
45    m_transformer = transformer;
46  }
47
48  /** Transformer instance          */
49  private TransformerImpl m_transformer;
50
51  /**
52   * Warn the user of a problem.
53   * This is public for access by extensions.
54   *
55   * @param msg The message text to issue
56   * @param terminate Flag indicating whether to terminate this process
57   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
58   * the error condition is severe enough to halt processing.
59   *
60   * @throws TransformerException
61   */
62  public void message(SourceLocator srcLctr, String msg, boolean terminate) throws TransformerException
63  {
64
65    ErrorListener errHandler = m_transformer.getErrorListener();
66
67    if (null != errHandler)
68    {
69      errHandler.warning(new TransformerException(msg, srcLctr));
70    }
71    else
72    {
73      if (terminate)
74        throw new TransformerException(msg, srcLctr);
75      else
76        System.out.println(msg);
77    }
78  }
79
80  /**
81   * Warn the user of a problem.
82   *
83   * @param msg Message text to issue
84   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
85   * the error condition is severe enough to halt processing.
86   *
87   * @throws TransformerException
88   * @xsl.usage internal
89   */
90  public void warn(SourceLocator srcLctr, String msg) throws TransformerException
91  {
92    warn(srcLctr, null, null, msg, null);
93  }
94
95  /**
96   * Warn the user of a problem.
97   *
98   * @param msg Message text to issue
99   * @param args Arguments to pass to the message
100   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
101   * the error condition is severe enough to halt processing.
102   *
103   * @throws TransformerException
104   * @xsl.usage internal
105   */
106  public void warn(SourceLocator srcLctr, String msg, Object[] args) throws TransformerException
107  {
108    warn(srcLctr, null, null, msg, args);
109  }
110
111  /**
112   * Warn the user of a problem.
113   *
114   *
115   * @param styleNode Stylesheet node
116   * @param sourceNode Source tree node
117   * @param msg Message text to issue
118   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
119   * the error condition is severe enough to halt processing.
120   *
121   * @throws TransformerException
122   * @xsl.usage internal
123   */
124  public void warn(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg)
125          throws TransformerException
126  {
127    warn(srcLctr, styleNode, sourceNode, msg, null);
128  }
129
130  /**
131   * Warn the user of a problem.
132   *
133   * @param styleNode Stylesheet node
134   * @param sourceNode Source tree node
135   * @param msg Message text to issue
136   * @param args Arguments to pass to the message
137   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
138   * the error condition is severe enough to halt processing.
139   *
140   * @throws TransformerException
141   * @xsl.usage internal
142   */
143  public void warn(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg, Object args[])
144          throws TransformerException
145  {
146
147    String formattedMsg = XSLMessages.createWarning(msg, args);
148    ErrorListener errHandler = m_transformer.getErrorListener();
149
150    if (null != errHandler)
151      errHandler.warning(new TransformerException(formattedMsg, srcLctr));
152    else
153      System.out.println(formattedMsg);
154  }
155
156  /* This method is not properly i18nized. We need to use the following method
157   * Tell the user of an error, and probably throw an
158   * exception.
159   *
160   * @param msg Message text to issue
161   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
162   * the error condition is severe enough to halt processing.
163   *
164   * @throws TransformerException
165   *
166  public void error(SourceLocator srcLctr, String msg) throws TransformerException
167  {
168
169    // Locator locator = m_stylesheetLocatorStack.isEmpty()
170    //                  ? null :
171    //                    ((Locator)m_stylesheetLocatorStack.peek());
172    // Locator locator = null;
173    ErrorListener errHandler = m_transformer.getErrorListener();
174
175    if (null != errHandler)
176      errHandler.fatalError(new TransformerException(msg, srcLctr));
177    else
178      throw new TransformerException(msg, srcLctr);
179  }
180
181 * @xsl.usage internal
182 */
183
184  /**
185   * Tell the user of an error, and probably throw an
186   * exception.
187   *
188   * @param msg Message text to issue
189   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
190   * the error condition is severe enough to halt processing.
191   *
192   * @throws TransformerException
193   * @xsl.usage internal
194   */
195  public void error(SourceLocator srcLctr, String msg) throws TransformerException
196  {
197    error(srcLctr, null, null, msg, null);
198  }
199
200  /**
201   * Tell the user of an error, and probably throw an
202   * exception.
203   *
204   * @param msg Message text to issue
205   * @param args Arguments to be passed to the message
206   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
207   * the error condition is severe enough to halt processing.
208   *
209   * @throws TransformerException
210   * @xsl.usage internal
211   */
212  public void error(SourceLocator srcLctr, String msg, Object[] args) throws TransformerException
213  {
214    error(srcLctr, null, null, msg, args);
215  }
216
217  /**
218   * Tell the user of an error, and probably throw an
219   * exception.
220   *
221   * @param msg Message text to issue
222   * @param e Exception to throw
223   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
224   * the error condition is severe enough to halt processing.
225   *
226   * @throws TransformerException
227   * @xsl.usage internal
228   */
229  public void error(SourceLocator srcLctr, String msg, Exception e) throws TransformerException
230  {
231    error(srcLctr, msg, null, e);
232  }
233
234  /**
235   * Tell the user of an error, and probably throw an
236   * exception.
237   *
238   * @param msg Message text to issue
239   * @param args Arguments to use in message
240   * @param e Exception to throw
241   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
242   * the error condition is severe enough to halt processing.
243   *
244   * @throws TransformerException
245   * @xsl.usage internal
246   */
247  public void error(SourceLocator srcLctr, String msg, Object args[], Exception e) throws TransformerException
248  {
249
250    //msg  = (null == msg) ? XSLTErrorResources.ER_PROCESSOR_ERROR : msg;
251    String formattedMsg = XSLMessages.createMessage(msg, args);
252
253    // Locator locator = m_stylesheetLocatorStack.isEmpty()
254    //                   ? null :
255    //                    ((Locator)m_stylesheetLocatorStack.peek());
256    // Locator locator = null;
257    ErrorListener errHandler = m_transformer.getErrorListener();
258
259    if (null != errHandler)
260      errHandler.fatalError(new TransformerException(formattedMsg, srcLctr));
261    else
262      throw new TransformerException(formattedMsg, srcLctr);
263  }
264
265  /**
266   * Tell the user of an error, and probably throw an
267   * exception.
268   *
269   * @param styleNode Stylesheet node
270   * @param sourceNode Source tree node
271   * @param msg Message text to issue
272   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
273   * the error condition is severe enough to halt processing.
274   *
275   * @throws TransformerException
276   * @xsl.usage internal
277   */
278  public void error(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg)
279          throws TransformerException
280  {
281    error(srcLctr, styleNode, sourceNode, msg, null);
282  }
283
284  /**
285   * Tell the user of an error, and probably throw an
286   * exception.
287   *
288   * @param styleNode Stylesheet node
289   * @param sourceNode Source tree node
290   * @param msg Message text to issue
291   * @param args Arguments to use in message
292   * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
293   * the error condition is severe enough to halt processing.
294   *
295   * @throws TransformerException
296   * @xsl.usage internal
297   */
298  public void error(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg, Object args[])
299          throws TransformerException
300  {
301
302    String formattedMsg = XSLMessages.createMessage(msg, args);
303
304    // Locator locator = m_stylesheetLocatorStack.isEmpty()
305    //                   ? null :
306    //                    ((Locator)m_stylesheetLocatorStack.peek());
307    // Locator locator = null;
308    ErrorListener errHandler = m_transformer.getErrorListener();
309
310    if (null != errHandler)
311      errHandler.fatalError(new TransformerException(formattedMsg, srcLctr));
312    else
313      throw new TransformerException(formattedMsg, srcLctr);
314  }
315}
316