1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.transformer;
  58. import org.apache.xalan.res.XSLMessages;
  59. import org.xml.sax.Locator;
  60. import org.xml.sax.helpers.LocatorImpl;
  61. import org.w3c.dom.Node;
  62. import javax.xml.transform.TransformerException;
  63. import javax.xml.transform.ErrorListener;
  64. import javax.xml.transform.dom.DOMLocator;
  65. import javax.xml.transform.SourceLocator;
  66. /**
  67. * This class will manage error messages, warning messages, and other types of
  68. * message events.
  69. */
  70. public class MsgMgr
  71. {
  72. /**
  73. * Create a message manager object.
  74. *
  75. * @param transformer non transformer instance
  76. */
  77. public MsgMgr(TransformerImpl transformer)
  78. {
  79. m_transformer = transformer;
  80. }
  81. /** Transformer instance */
  82. private TransformerImpl m_transformer;
  83. /** XSLMessages instance, sets things up for issuing messages */
  84. private static XSLMessages m_XSLMessages = new XSLMessages();
  85. /**
  86. * Warn the user of a problem.
  87. * This is public for access by extensions.
  88. *
  89. * @param msg The message text to issue
  90. * @param terminate Flag indicating whether to terminate this process
  91. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  92. * the error condition is severe enough to halt processing.
  93. *
  94. * @throws TransformerException
  95. */
  96. public void message(SourceLocator srcLctr, String msg, boolean terminate) throws TransformerException
  97. {
  98. ErrorListener errHandler = m_transformer.getErrorListener();
  99. if (null != errHandler)
  100. {
  101. errHandler.warning(new TransformerException(msg, srcLctr));
  102. }
  103. else
  104. {
  105. if (terminate)
  106. throw new TransformerException(msg, srcLctr);
  107. else
  108. System.out.println(msg);
  109. }
  110. }
  111. /**
  112. * <meta name="usage" content="internal"/>
  113. * Warn the user of a problem.
  114. *
  115. * @param msg Message text to issue
  116. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  117. * the error condition is severe enough to halt processing.
  118. *
  119. * @throws TransformerException
  120. */
  121. public void warn(SourceLocator srcLctr, String msg) throws TransformerException
  122. {
  123. warn(srcLctr, null, null, msg, null);
  124. }
  125. /**
  126. * <meta name="usage" content="internal"/>
  127. * Warn the user of a problem.
  128. *
  129. * @param msg Message text to issue
  130. * @param args Arguments to pass to the message
  131. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  132. * the error condition is severe enough to halt processing.
  133. *
  134. * @throws TransformerException
  135. */
  136. public void warn(SourceLocator srcLctr, String msg, Object[] args) throws TransformerException
  137. {
  138. warn(srcLctr, null, null, msg, args);
  139. }
  140. /**
  141. * <meta name="usage" content="internal"/>
  142. * Warn the user of a problem.
  143. *
  144. *
  145. * @param styleNode Stylesheet node
  146. * @param sourceNode Source tree node
  147. * @param msg Message text to issue
  148. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  149. * the error condition is severe enough to halt processing.
  150. *
  151. * @throws TransformerException
  152. */
  153. public void warn(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg)
  154. throws TransformerException
  155. {
  156. warn(srcLctr, styleNode, sourceNode, msg, null);
  157. }
  158. /**
  159. * <meta name="usage" content="internal"/>
  160. * Warn the user of a problem.
  161. *
  162. * @param styleNode Stylesheet node
  163. * @param sourceNode Source tree node
  164. * @param msg Message text to issue
  165. * @param args Arguments to pass to the message
  166. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  167. * the error condition is severe enough to halt processing.
  168. *
  169. * @throws TransformerException
  170. */
  171. public void warn(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg, Object args[])
  172. throws TransformerException
  173. {
  174. String formattedMsg = m_XSLMessages.createWarning(msg, args);
  175. ErrorListener errHandler = m_transformer.getErrorListener();
  176. if (null != errHandler)
  177. errHandler.warning(new TransformerException(formattedMsg, srcLctr));
  178. else
  179. System.out.println(formattedMsg);
  180. }
  181. /* This method is not properly i18nized. We need to use the following method
  182. * <meta name="usage" content="internal"/>
  183. * Tell the user of an error, and probably throw an
  184. * exception.
  185. *
  186. * @param msg Message text to issue
  187. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  188. * the error condition is severe enough to halt processing.
  189. *
  190. * @throws TransformerException
  191. *
  192. public void error(SourceLocator srcLctr, String msg) throws TransformerException
  193. {
  194. // Locator locator = m_stylesheetLocatorStack.isEmpty()
  195. // ? null :
  196. // ((Locator)m_stylesheetLocatorStack.peek());
  197. // Locator locator = null;
  198. ErrorListener errHandler = m_transformer.getErrorListener();
  199. if (null != errHandler)
  200. errHandler.fatalError(new TransformerException(msg, srcLctr));
  201. else
  202. throw new TransformerException(msg, srcLctr);
  203. }
  204. */
  205. /**
  206. * <meta name="usage" content="internal"/>
  207. * Tell the user of an error, and probably throw an
  208. * exception.
  209. *
  210. * @param msg Message text to issue
  211. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  212. * the error condition is severe enough to halt processing.
  213. *
  214. * @throws TransformerException
  215. */
  216. public void error(SourceLocator srcLctr, String msg) throws TransformerException
  217. {
  218. error(srcLctr, null, null, msg, null);
  219. }
  220. /**
  221. * <meta name="usage" content="internal"/>
  222. * Tell the user of an error, and probably throw an
  223. * exception.
  224. *
  225. * @param msg Message text to issue
  226. * @param args Arguments to be passed to the message
  227. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  228. * the error condition is severe enough to halt processing.
  229. *
  230. * @throws TransformerException
  231. */
  232. public void error(SourceLocator srcLctr, String msg, Object[] args) throws TransformerException
  233. {
  234. error(srcLctr, null, null, msg, args);
  235. }
  236. /**
  237. * <meta name="usage" content="internal"/>
  238. * Tell the user of an error, and probably throw an
  239. * exception.
  240. *
  241. * @param msg Message text to issue
  242. * @param e Exception to throw
  243. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  244. * the error condition is severe enough to halt processing.
  245. *
  246. * @throws TransformerException
  247. */
  248. public void error(SourceLocator srcLctr, String msg, Exception e) throws TransformerException
  249. {
  250. error(srcLctr, msg, null, e);
  251. }
  252. /**
  253. * <meta name="usage" content="internal"/>
  254. * Tell the user of an error, and probably throw an
  255. * exception.
  256. *
  257. * @param msg Message text to issue
  258. * @param args Arguments to use in message
  259. * @param e Exception to throw
  260. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  261. * the error condition is severe enough to halt processing.
  262. *
  263. * @throws TransformerException
  264. */
  265. public void error(SourceLocator srcLctr, String msg, Object args[], Exception e) throws TransformerException
  266. {
  267. //msg = (null == msg) ? XSLTErrorResources.ER_PROCESSOR_ERROR : msg;
  268. String formattedMsg = m_XSLMessages.createMessage(msg, args);
  269. // Locator locator = m_stylesheetLocatorStack.isEmpty()
  270. // ? null :
  271. // ((Locator)m_stylesheetLocatorStack.peek());
  272. // Locator locator = null;
  273. ErrorListener errHandler = m_transformer.getErrorListener();
  274. if (null != errHandler)
  275. errHandler.fatalError(new TransformerException(formattedMsg, srcLctr));
  276. else
  277. throw new TransformerException(formattedMsg, srcLctr);
  278. }
  279. /**
  280. * <meta name="usage" content="internal"/>
  281. * Tell the user of an error, and probably throw an
  282. * exception.
  283. *
  284. * @param styleNode Stylesheet node
  285. * @param sourceNode Source tree node
  286. * @param msg Message text to issue
  287. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  288. * the error condition is severe enough to halt processing.
  289. *
  290. * @throws TransformerException
  291. */
  292. public void error(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg)
  293. throws TransformerException
  294. {
  295. error(srcLctr, styleNode, sourceNode, msg, null);
  296. }
  297. /**
  298. * <meta name="usage" content="internal"/>
  299. * Tell the user of an error, and probably throw an
  300. * exception.
  301. *
  302. * @param styleNode Stylesheet node
  303. * @param sourceNode Source tree node
  304. * @param msg Message text to issue
  305. * @param args Arguments to use in message
  306. * @throws XSLProcessorException thrown if the active ProblemListener and XPathContext decide
  307. * the error condition is severe enough to halt processing.
  308. *
  309. * @throws TransformerException
  310. */
  311. public void error(SourceLocator srcLctr, Node styleNode, Node sourceNode, String msg, Object args[])
  312. throws TransformerException
  313. {
  314. String formattedMsg = m_XSLMessages.createMessage(msg, args);
  315. // Locator locator = m_stylesheetLocatorStack.isEmpty()
  316. // ? null :
  317. // ((Locator)m_stylesheetLocatorStack.peek());
  318. // Locator locator = null;
  319. ErrorListener errHandler = m_transformer.getErrorListener();
  320. if (null != errHandler)
  321. errHandler.fatalError(new TransformerException(formattedMsg, srcLctr));
  322. else
  323. throw new TransformerException(formattedMsg, srcLctr);
  324. }
  325. }