001/* 002 * Copyright 2011 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.skinfactory.generators; 017 018import java.io.IOException; 019import java.io.InputStream; 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.ServiceableGenerator; 026import org.apache.cocoon.xml.XMLUtils; 027import org.apache.excalibur.source.Source; 028import org.apache.excalibur.xml.sax.SAXParser; 029import org.xml.sax.InputSource; 030import org.xml.sax.SAXException; 031 032import org.ametys.core.util.IgnoreRootHandler; 033 034/** 035 * Generates the model file with the modifications 036 */ 037public class ChangeModelHash extends ServiceableGenerator 038{ 039 @Override 040 public void generate() throws IOException, SAXException, ProcessingException 041 { 042 contentHandler.startDocument(); 043 XMLUtils.startElement(contentHandler, "CMS"); 044 045 Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 046 if (parentContextParameters != null) 047 { 048 Source src = resolver.resolveURI((String) parentContextParameters.get("modelUri")); 049 SAXParser saxParser = null; 050 try (InputStream is = src.getInputStream()) 051 { 052 saxParser = (SAXParser) manager.lookup(SAXParser.ROLE); 053 saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler)); 054 } 055 catch (ServiceException e) 056 { 057 throw new ProcessingException("Unable to get a SAX parser.", e); 058 } 059 finally 060 { 061 resolver.release(src); 062 manager.release(saxParser); 063 } 064 065 String hash = (String) parentContextParameters.get("hash"); 066 XMLUtils.createElement(contentHandler, "hash", hash); 067 } 068 069 XMLUtils.endElement(contentHandler, "CMS"); 070 contentHandler.endDocument(); 071 072 } 073 074}