| 1 | /* |
| 2 | * Copyright 2005-2006 The RbUtils Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | // $Id: AbstractXmlLogger.java,v 1.5 2006/10/27 22:11:40 moishi Exp $ |
| 19 | |
| 20 | package org.ktc.rbutils.api.audit; |
| 21 | |
| 22 | import java.io.BufferedOutputStream; |
| 23 | import java.io.File; |
| 24 | import java.io.FileOutputStream; |
| 25 | import java.io.IOException; |
| 26 | import java.io.OutputStream; |
| 27 | import java.io.OutputStreamWriter; |
| 28 | import java.io.PrintWriter; |
| 29 | import java.io.StringWriter; |
| 30 | import java.io.UnsupportedEncodingException; |
| 31 | import org.apache.commons.io.IOUtils; |
| 32 | import org.ktc.rbutils.RbUtilsHelper; |
| 33 | import org.ktc.rbutils.api.StringTools; |
| 34 | import org.ktc.rbutils.api.audit.event.ProcessEndEvent; |
| 35 | import org.ktc.rbutils.api.audit.event.ProcessEvent; |
| 36 | import org.ktc.rbutils.api.audit.event.ProcessFileEndEvent; |
| 37 | import org.ktc.rbutils.api.audit.event.ProcessFileStartEvent; |
| 38 | import org.ktc.rbutils.api.audit.event.ProcessStartEvent; |
| 39 | import org.ktc.rbutils.api.file.FileTools; |
| 40 | import org.ktc.rbutils.api.file.ValidateFile; |
| 41 | |
| 42 | /** |
| 43 | * Abstract class for xml logging. It outputs everything in UTF-8 (default XML encoding is UTF-8) in |
| 44 | * case we want to localize error messages or simply that filenames are localized and takes care |
| 45 | * about escaping as well. |
| 46 | * @since RbUtils 0.8.2 |
| 47 | * @version $Revision: 1.5 $ |
| 48 | * @author moishi |
| 49 | * @author ktcguru |
| 50 | */ |
| 51 | public abstract class AbstractXmlLogger implements Logger { |
| 52 | /** Name of the task that this logger is inspecting. */ |
| 53 | protected String taskName; |
| 54 | |
| 55 | /** Close output stream when process completed. */ |
| 56 | protected boolean closeStream; |
| 57 | /** Helper writer that allows easy encoding and printing. */ |
| 58 | protected PrintWriter writer; |
| 59 | /** Represents a tabulation character; 4 spaces. */ |
| 60 | protected static final String TAB = " "; |
| 61 | /** Represents two tabulation characters; 8 spaces. */ |
| 62 | protected static final String TAB_2 = TAB + TAB; |
| 63 | /** Double quote constant. */ |
| 64 | protected static final String DBLEQUOT = "\""; |
| 65 | /** Closing character of a node. */ |
| 66 | protected static final String CLOSENODE = ">"; |
| 67 | /** Closing character of the end of a node. */ |
| 68 | protected static final String CLOSEENDNODE = "/>"; |
| 69 | /** Encoding used by this logger. This is the UTF-8 encoding. */ |
| 70 | protected static final String ENCODING = "UTF-8"; |
| 71 | |
| 72 | /** |
| 73 | * Constructs a new Logger. |
| 74 | * In order to clean the xmlLogFile, it is first deleted, then it is created. |
| 75 | * @param xmlLogFile file to be used to store the logs. |
| 76 | * @throws org.apache.commons.lang.NullArgumentException if xmlLogFile is <code>null</code>. |
| 77 | * @throws IOException if it is not possible to create the file. |
| 78 | */ |
| 79 | protected AbstractXmlLogger(final File xmlLogFile) throws IOException { |
| 80 | // Validate argument |
| 81 | ValidateFile.notNull(xmlLogFile); |
| 82 | |
| 83 | // Delete the xml file and create all directories to this file |
| 84 | FileTools.forceNewFile(xmlLogFile); |
| 85 | |
| 86 | // Instanciate the logger |
| 87 | final OutputStream os = new FileOutputStream(xmlLogFile); |
| 88 | setOutputStream(new BufferedOutputStream(os)); |
| 89 | closeStream = true; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets the OutputStream. |
| 94 | * @param os the OutputStream to use |
| 95 | */ |
| 96 | protected void setOutputStream(final OutputStream os) { |
| 97 | try { |
| 98 | final OutputStreamWriter osw = new OutputStreamWriter(os, ENCODING); |
| 99 | writer = new PrintWriter(osw); |
| 100 | } |
| 101 | catch (final UnsupportedEncodingException e) { |
| 102 | // this should never happened |
| 103 | throw new ExceptionInInitializerError(e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** {@inheritDoc} */ |
| 108 | public void addException(final Exception exception) { |
| 109 | |
| 110 | final String message = exception.getMessage(); |
| 111 | writer.println(TAB_2 + "<exception message=" + DBLEQUOT + message + DBLEQUOT + CLOSENODE); |
| 112 | writer.println(TAB_2 + "<![CDATA["); |
| 113 | final StringWriter sw = new StringWriter(); |
| 114 | final PrintWriter pw = new PrintWriter(sw); |
| 115 | exception.printStackTrace(pw); |
| 116 | pw.flush(); |
| 117 | writer.println(StringTools.escapeHtml(sw.toString())); |
| 118 | writer.println(TAB_2 + "]]>"); |
| 119 | writer.println(TAB_2 + "</exception>"); |
| 120 | } |
| 121 | |
| 122 | // protected String getCdataString(final String message) { |
| 123 | // return "CDATA[" + message + "]>"; |
| 124 | // } |
| 125 | |
| 126 | /** |
| 127 | * Returns the header of an UTF-8 xml file. |
| 128 | * @return the header of an UTF-8 xml file. |
| 129 | */ |
| 130 | protected String getXmlHeader() { |
| 131 | return "<?xml version=" + DBLEQUOT + "1.0" + DBLEQUOT + " encoding=" + DBLEQUOT + ENCODING |
| 132 | + DBLEQUOT + "?>"; |
| 133 | } |
| 134 | |
| 135 | /** {@inheritDoc} */ |
| 136 | public void processEnded(final ProcessEndEvent event) { |
| 137 | writer.println("</" + taskName + CLOSENODE); |
| 138 | writer.flush(); |
| 139 | if (closeStream) { |
| 140 | IOUtils.closeQuietly(writer); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** {@inheritDoc} */ |
| 145 | public void processFileEnded(final ProcessFileEndEvent event) { |
| 146 | writer.println(TAB + "</file>"); |
| 147 | } |
| 148 | |
| 149 | /** {@inheritDoc} */ |
| 150 | public void processStarted(final ProcessStartEvent event) { |
| 151 | final String version = RbUtilsHelper.getVersion(); |
| 152 | writer.println(getXmlHeader()); |
| 153 | writer.println(); |
| 154 | writer.println("<" + taskName + " version=" + DBLEQUOT + version + DBLEQUOT + " root=" |
| 155 | + DBLEQUOT + event.getRootDirectory().getAbsolutePath() + DBLEQUOT + " generatedOn=" |
| 156 | + DBLEQUOT + event.getEventDate() + DBLEQUOT + CLOSENODE); |
| 157 | } |
| 158 | |
| 159 | /** {@inheritDoc} */ |
| 160 | public void processFileStarted(final ProcessFileStartEvent event) { |
| 161 | writer.println(TAB + "<file name=" + DBLEQUOT + event.getFileName() + DBLEQUOT |
| 162 | + " classname=" + DBLEQUOT + event.getClassName() + DBLEQUOT + " locale=" + DBLEQUOT |
| 163 | + event.getLocale().toString() + DBLEQUOT + CLOSENODE); |
| 164 | } |
| 165 | |
| 166 | /** {@inheritDoc} */ |
| 167 | public void addError(final ProcessEvent event) { |
| 168 | writer.print(TAB_2 + "<error"); |
| 169 | |
| 170 | writer.print(" errorType=" + DBLEQUOT + event.getDetailledMessage() + DBLEQUOT); |
| 171 | writer.print(" message=" + DBLEQUOT + event.getMessageDisplay() + DBLEQUOT); |
| 172 | writer.println(CLOSEENDNODE); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Does nothing. Lets subclasses implement this method is needed. {@inheritDoc} |
| 177 | */ |
| 178 | public void addInfo(final ProcessEvent event) { |
| 179 | // Do Nothing |
| 180 | } |
| 181 | |
| 182 | } |