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.avalon.framework.service.ServiceManager; 024import org.apache.cocoon.ProcessingException; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.apache.excalibur.source.Source; 030import org.apache.excalibur.xml.sax.SAXParser; 031import org.xml.sax.InputSource; 032import org.xml.sax.SAXException; 033 034import org.ametys.core.util.IgnoreRootHandler; 035import org.ametys.skinfactory.parameters.ImageParameter.FileValue; 036 037/** 038 * Generates the model file with the modifications 039 */ 040public class ChangeParameters extends ServiceableGenerator 041{ 042 private SAXParser _saxParser; 043 044 @Override 045 public void service(ServiceManager smanager) throws ServiceException 046 { 047 super.service(smanager); 048 _saxParser = (SAXParser) manager.lookup(SAXParser.ROLE); 049 } 050 051 @Override 052 public void generate() throws IOException, SAXException, ProcessingException 053 { 054 contentHandler.startDocument(); 055 XMLUtils.startElement(contentHandler, "CMS"); 056 057 Map parentContextParameters = (Map) objectModel.get(ObjectModelHelper.PARENT_CONTEXT); 058 if (parentContextParameters != null) 059 { 060 Source src = null; 061 src = resolver.resolveURI((String) parentContextParameters.get("modelUri")); 062 try (InputStream is = src.getInputStream()) 063 { 064 _saxParser.parse(new InputSource(is), new IgnoreRootHandler(contentHandler)); 065 } 066 finally 067 { 068 resolver.release(src); 069 } 070 071 XMLUtils.startElement(contentHandler, "settings"); 072 073 @SuppressWarnings("unchecked") 074 Map<String, Object> params = (Map<String, Object>) parentContextParameters.get("skinParameters"); 075 076 for (String paramId : params.keySet()) 077 { 078 AttributesImpl attrs = new AttributesImpl(); 079 attrs.addCDATAAttribute("id", paramId); 080 081 Object value = params.get(paramId); 082 if (value instanceof String) 083 { 084 XMLUtils.createElement(contentHandler, "param", attrs, (String) value); 085 } 086 else if (value instanceof FileValue) 087 { 088 attrs.addCDATAAttribute("uploaded", String.valueOf(((FileValue) value).isUploaded())); 089 XMLUtils.createElement(contentHandler, "param", attrs, ((FileValue) value).getPath()); 090 } 091 else 092 { 093 @SuppressWarnings("unchecked") 094 Map<String, String> mapValues = (Map<String, String>) value; 095 XMLUtils.startElement(contentHandler, "param", attrs); 096 097 for (String lang : mapValues.keySet()) 098 { 099 XMLUtils.createElement(contentHandler, lang, mapValues.get(lang)); 100 } 101 XMLUtils.endElement(contentHandler, "param"); 102 } 103 104 } 105 XMLUtils.endElement(contentHandler, "settings"); 106 } 107 108 XMLUtils.endElement(contentHandler, "CMS"); 109 contentHandler.endDocument(); 110 111 } 112 113}