You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by si...@apache.org on 2010/04/22 21:01:58 UTC
svn commit: r937012 - in /cocoon/cocoon3/trunk/cocoon-sax/src:
main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java
Author: simonetripodi
Date: Thu Apr 22 19:01:58 2010
New Revision: 937012
URL: http://svn.apache.org/viewvc?rev=937012&view=rev
Log:
first checkin of LogAsXMLTransformer class
Added:
cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java (with props)
cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java (with props)
Added: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java?rev=937012&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java Thu Apr 22 19:01:58 2010
@@ -0,0 +1,210 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cocoon.sax.component;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.cocoon.pipeline.SetupException;
+import org.apache.cocoon.sax.AbstractSAXTransformer;
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+/**
+ *
+ * This class is not thread-safety!
+ */
+public final class LogAsXMLTransformer extends AbstractSAXTransformer {
+
+ private static final String LOG_FILE = "logfile";
+
+ private final XMLSerializer xmlSerializer = XMLSerializer.createXMLSerializer();
+
+ public LogAsXMLTransformer() {
+ this.init(System.out);
+ }
+
+ public LogAsXMLTransformer(File logFile) {
+ if (logFile == null) {
+ throw new IllegalArgumentException("Argument 'logFile' must not be null");
+ }
+
+ try {
+ this.init(new FileOutputStream(logFile));
+ } catch (FileNotFoundException e) {
+ throw new SetupException("Impossible to create an XML log file '"
+ + logFile
+ + "'");
+ }
+ }
+
+ private void init(OutputStream outputStream) {
+ this.xmlSerializer.setup(new HashMap<String, Object>());
+ this.xmlSerializer.setOutputStream(outputStream);
+ }
+
+ @Override
+ public void setup(Map<String, Object> parameters) {
+ if (parameters == null) {
+ return;
+ }
+
+ Object logFileString = parameters.get(LOG_FILE);
+
+ OutputStream outputStream = null;
+ if (logFileString != null) {
+ File logFile = new File(String.valueOf(logFileString));
+ try {
+ outputStream = new FileOutputStream(logFile);
+ } catch (FileNotFoundException e) {
+ throw new SetupException("Impossible to open log file '"
+ + logFile
+ + "'", e);
+ }
+ }
+
+ this.xmlSerializer.setOutputStream(outputStream);
+ }
+
+ @Override
+ public void finish() {
+ this.xmlSerializer.finish();
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length)
+ throws SAXException {
+ this.xmlSerializer.characters(ch, start, length);
+ super.characters(ch, start, length);
+ }
+
+ @Override
+ public void comment(char[] ch, int start, int length) throws SAXException {
+ this.xmlSerializer.comment(ch, start, length);
+ super.comment(ch, start, length);
+ }
+
+ @Override
+ public void endCDATA() throws SAXException {
+ this.xmlSerializer.endCDATA();
+ super.endCDATA();
+ }
+
+ @Override
+ public void endDocument() throws SAXException {
+ this.xmlSerializer.endDocument();
+ super.endDocument();
+ }
+
+ @Override
+ public void endDTD() throws SAXException {
+ this.xmlSerializer.endDTD();
+ super.endDTD();
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String name)
+ throws SAXException {
+ this.xmlSerializer.endElement(uri, localName, name);
+ super.endElement(uri, localName, name);
+ }
+
+ @Override
+ public void endEntity(String name) throws SAXException {
+ this.xmlSerializer.endEntity(name);
+ super.endEntity(name);
+ }
+
+ @Override
+ public void endPrefixMapping(String prefix) throws SAXException {
+ this.xmlSerializer.endPrefixMapping(prefix);
+ super.endPrefixMapping(prefix);
+ }
+
+ @Override
+ public void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException {
+ this.xmlSerializer.ignorableWhitespace(ch, start, length);
+ super.ignorableWhitespace(ch, start, length);
+ }
+
+ @Override
+ public void processingInstruction(String target, String data)
+ throws SAXException {
+ this.xmlSerializer.processingInstruction(target, data);
+ super.processingInstruction(target, data);
+ }
+
+ @Override
+ public void setDocumentLocator(Locator locator) {
+ this.xmlSerializer.setDocumentLocator(locator);
+ super.setDocumentLocator(locator);
+ }
+
+ @Override
+ public void skippedEntity(String name) throws SAXException {
+ this.xmlSerializer.skippedEntity(name);
+ super.skippedEntity(name);
+ }
+
+ @Override
+ public void startCDATA() throws SAXException {
+ this.xmlSerializer.startCDATA();
+ super.startCDATA();
+ }
+
+ @Override
+ public void startDocument() throws SAXException {
+ this.xmlSerializer.startDocument();
+ super.startDocument();
+ }
+
+ @Override
+ public void startDTD(String name, String publicId, String systemId)
+ throws SAXException {
+ this.xmlSerializer.startDTD(name, publicId, systemId);
+ super.startDTD(name, publicId, systemId);
+ }
+
+ @Override
+ public void startElement(String uri, String localName, String name,
+ Attributes atts) throws SAXException {
+ this.xmlSerializer.startElement(uri, localName, name, atts);
+ super.startElement(uri, localName, name, atts);
+ }
+
+ @Override
+ public void startEntity(String name) throws SAXException {
+ this.xmlSerializer.startEntity(name);
+ super.startEntity(name);
+ }
+
+ @Override
+ public void startPrefixMapping(String prefix, String uri)
+ throws SAXException {
+ this.xmlSerializer.startPrefixMapping(prefix, uri);
+ super.startPrefixMapping(prefix, uri);
+ }
+
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LogAsXMLTransformer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java?rev=937012&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java (added)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java Thu Apr 22 19:01:58 2010
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.sax.component;
+
+import java.io.ByteArrayOutputStream;
+
+import org.apache.cocoon.pipeline.NonCachingPipeline;
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.sax.SAXPipelineComponent;
+import org.junit.Test;
+
+public final class LogAsXMLTransformerTestCase {
+
+ @Test
+ public void logToSystemOut() throws Exception {
+ Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
+ pipeline.addComponent(new XMLGenerator(this.getClass().getResource("/test.xml")));
+ pipeline.addComponent(new LogAsXMLTransformer());
+ pipeline.addComponent(new XMLSerializer());
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ pipeline.setup(baos);
+ pipeline.execute();
+ }
+
+}
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LogAsXMLTransformerTestCase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain