001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.transformation; 017 018import java.io.IOException; 019import java.util.Map; 020 021import org.apache.avalon.excalibur.pool.Recyclable; 022import org.apache.avalon.framework.parameters.Parameters; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.cocoon.ProcessingException; 027import org.apache.cocoon.environment.SourceResolver; 028import org.apache.cocoon.transformation.Transformer; 029import org.apache.cocoon.xml.XMLConsumer; 030import org.xml.sax.Attributes; 031import org.xml.sax.ContentHandler; 032import org.xml.sax.Locator; 033import org.xml.sax.SAXException; 034import org.xml.sax.ext.LexicalHandler; 035 036/** 037 * This abstract cocoon transformer have its content and lexical handler configurable 038 */ 039public abstract class AbstractEnhancementTransformer implements Transformer, Serviceable, Recyclable 040{ 041 /** The service manager */ 042 protected ServiceManager _manager; 043 /** The content handler */ 044 protected ContentHandler _contentHandler; 045 /** The lexical handler */ 046 protected LexicalHandler _lexicalHandler; 047 /** The output format (html, md) */ 048 protected String _outputFormat; 049 050 @Override 051 public void recycle() 052 { 053 _contentHandler = null; 054 _lexicalHandler = null; 055 } 056 057 @Override 058 public abstract void setConsumer(XMLConsumer consumer); 059 060 061 public void service(ServiceManager manager) throws ServiceException 062 { 063 _manager = manager; 064 } 065 066 @Override 067 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) throws ProcessingException, SAXException, IOException 068 { 069 _outputFormat = par.getParameter("format", "html"); 070 } 071 072 @Override 073 public void startDocument() throws SAXException 074 { 075 _contentHandler.startDocument(); 076 } 077 @Override 078 public void endDocument() throws SAXException 079 { 080 _contentHandler.endDocument(); 081 } 082 083 @Override 084 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException 085 { 086 _contentHandler.startElement(uri, localName, qName, atts); 087 } 088 @Override 089 public void endElement(String uri, String localName, String qName) throws SAXException 090 { 091 _contentHandler.endElement(uri, localName, qName); 092 } 093 094 @Override 095 public void characters(char[] ch, int start, int length) throws SAXException 096 { 097 _contentHandler.characters(ch, start, length); 098 } 099 @Override 100 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException 101 { 102 _contentHandler.ignorableWhitespace(ch, start, length); 103 } 104 105 @Override 106 public void startPrefixMapping(String prefix, String uri) throws SAXException 107 { 108 _contentHandler.startPrefixMapping(prefix, uri); 109 } 110 @Override 111 public void endPrefixMapping(String prefix) throws SAXException 112 { 113 _contentHandler.endPrefixMapping(prefix); 114 } 115 116 @Override 117 public void processingInstruction(String target, String data) throws SAXException 118 { 119 _contentHandler.processingInstruction(target, data); 120 } 121 122 @Override 123 public void setDocumentLocator(Locator locator) 124 { 125 _contentHandler.setDocumentLocator(locator); 126 } 127 128 @Override 129 public void skippedEntity(String name) throws SAXException 130 { 131 _contentHandler.skippedEntity(name); 132 } 133 134 @Override 135 public void comment(char[] ch, int start, int length) throws SAXException 136 { 137 _lexicalHandler.comment(ch, start, length); 138 } 139 140 @Override 141 public void startCDATA() throws SAXException 142 { 143 _lexicalHandler.startCDATA(); 144 } 145 @Override 146 public void endCDATA() throws SAXException 147 { 148 _lexicalHandler.endCDATA(); 149 } 150 151 @Override 152 public void startDTD(String name, String publicId, String systemId) throws SAXException 153 { 154 _lexicalHandler.startDTD(name, publicId, systemId); 155 } 156 @Override 157 public void endDTD() throws SAXException 158 { 159 _lexicalHandler.endDTD(); 160 } 161 162 @Override 163 public void startEntity(String name) throws SAXException 164 { 165 _lexicalHandler.startEntity(name); 166 } 167 @Override 168 public void endEntity(String name) throws SAXException 169 { 170 _lexicalHandler.endEntity(name); 171 } 172}