KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > xml > PrintWriterLog_Helper


1 package com.protomatter.syslog.xml;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.*;
54 import java.net.*;
55 import java.util.*;
56 import java.text.*;
57
58 import com.protomatter.xml.*;
59 import com.protomatter.syslog.*;
60 import org.jdom.*;
61
62 /**
63  * XML configuration helper for <tt>PrintWriterLog</tt>.
64  */

65 public class PrintWriterLog_Helper
66 extends BasicLogger_Helper
67 {
68   /**
69    * Configure this logger given the XML element.
70    * The <tt>&lt;Logger&gt;</tt> element should look like this:<P>
71    *
72    * <TABLE BORDER=1 CELLPADDING=4 CELLSPACING=0 WIDTH="90%">
73    * <TR><TD>
74    * <PRE><B>
75    *
76    * &lt;Logger class="com.protomatter.syslog.PrintWriterLog" &gt;
77    *
78    * <font color="#888888">&lt;!--
79    * Config params from {@link BasicLogger_Helper#configure(Object,Element) BasicLogger_Helper} can
80    * get inserted here.
81    * --&gt;</font>
82    *
83    * &lt;stream&gt;<i>StreamName</i>&lt;/stream&gt;
84    *
85    * &lt;/Logger&gt;
86    * </B></PRE>
87    * </TD></TR></TABLE><P>
88    *
89    * <TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0 WIDTH="90%">
90    * <TR CLASS="TableHeadingColor">
91    * <TD COLSPAN=3><B>Element</B></TD>
92    * </TR>
93    * <TR CLASS="TableHeadingColor">
94    * <TD><B>name</B></TD>
95    * <TD><B>value</B></TD>
96    * <TD><B>required</B></TD>
97    * </TR>
98    *
99    * <TR CLASS="TableRowColor">
100    * <TD VALIGN=TOP><TT>stream</TT></TD>
101    * <TD>The stream to attach to. Must be either
102    * "<tt>System.out</tt>" or "<tt>System.err</tt>".
103    * </TD>
104    * <TD VALIGN=TOP>yes</TD>
105    * </TR>
106    *
107    * </TABLE><P>
108    */

109    public void configure(Object JavaDoc o, Element e)
110    throws SyslogInitException
111    {
112      super.configure(o, e);
113
114      PrintWriterLog log = (PrintWriterLog)o;
115
116      boolean setStream = false;
117
118      String JavaDoc value = e.getChildTextTrim("stream", e.getNamespace());
119      if (value != null)
120      {
121        if (value.equals("System.out"))
122        {
123          log.setWriter(new PrintWriter(System.out));
124          log.setStreamName(value);
125        }
126        else if (value.equals("System.err"))
127        {
128          log.setWriter(new PrintWriter(System.err));
129          log.setStreamName(value);
130        }
131        else
132          throw new IllegalArgumentException JavaDoc(MessageFormat.format(
133            Syslog.getResourceString(MessageConstants.PARAM_MUST_BE_A_OR_B_MESSAGE),
134            new Object JavaDoc[] { "stream", "System.out", "System.err" } ));
135      }
136      else
137      {
138        throw new IllegalArgumentException JavaDoc(MessageFormat.format(
139            Syslog.getResourceString(MessageConstants.XML_MUST_SPECIFY_PARAM_MESSAGE),
140            new Object JavaDoc[] { "stream" } ));
141      }
142    }
143
144   public Element getConfiguration(Object JavaDoc o, Element element)
145   {
146     Element e = super.getConfiguration(o, element);
147
148     PrintWriterLog log = (PrintWriterLog)o;
149     String JavaDoc streamName = log.getStreamName();
150     if ("System.out".equals(streamName) || "System.err".equals(streamName))
151     {
152       e.getChildren().add((new Element("stream")).setText(streamName));
153     }
154     else
155     {
156       e.getChildren().clear();
157       Comment comment = new Comment(
158         "This PrintWriterLog instance was attached to a PrintWriter\n" +
159         "other than System.out or System.err, and so the configuration\n" +
160         "cannot be stored in an XML representation. Sorry.");
161       e.getChildren().add(comment);
162     }
163
164     return e;
165   }
166 }
167
Popular Tags