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.io.StringReader; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.generation.Generator; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.excalibur.xml.sax.SAXParser; 028import org.xml.sax.InputSource; 029import org.xml.sax.SAXException; 030 031/** 032 * {@link Generator} which uses a string from parent context 033 * and SAX it. 034 * TODO use NekoHTML in case of an invalid XML input. 035 */ 036public class XhtmlGenerator extends ServiceableGenerator 037{ 038 public void generate() throws IOException, SAXException, ProcessingException 039 { 040 SAXParser parser = null; 041 042 try 043 { 044 Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 045 046 if (parentContextParameters == null) 047 { 048 throw new ProcessingException("Missing parent context"); 049 } 050 051 String xmlSource = (String) parentContextParameters.get("source"); 052 053 if (xmlSource == null) 054 { 055 throw new ProcessingException("Missing source parent context attr"); 056 } 057 058 InputSource inputSource = new InputSource(new StringReader("<body>" + xmlSource + "</body>")); 059 parser = (SAXParser) this.manager.lookup(SAXParser.ROLE); 060 parser.parse(inputSource, super.xmlConsumer); 061 } 062 catch (ServiceException e) 063 { 064 throw new ProcessingException("Unable to get a SAX parser", e); 065 } 066 finally 067 { 068 this.manager.release(parser); 069 } 070 } 071}