/* Copyright 2006 - 2010 Under Dusken Licensed 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.pegadi.publisher; import org.pegadi.model.Article; import org.pegadi.model.LoginContext; import org.pegadi.model.PublishingMediaEnum; import org.pegadi.model.Stylesheet; import org.pegadi.publisher.io.CRLFFilterOutputStream; import org.pegadi.server.NoAccessException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.TransformerException; import java.io.File; import java.io.IOException; import java.util.Properties; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public abstract class AbstractPublisher implements Publisher { protected Logger log = LoggerFactory.getLogger(getClass()); private String xmlPath; private File file; private OutputEncoding encoding = OutputEncoding.utf8; private OutputMethod outputMethod = OutputMethod.xml; private CRLFFilterOutputStream.Mode mode = CRLFFilterOutputStream.Mode.WINDOWS; public boolean publish(Article article, File file, PublishingMediaEnum publishingMedia) throws TransformerException, IOException { Stylesheet stylesheet = getStylesheet(article, publishingMedia); setXmlPath(); this.file = file; return doPublish(article, stylesheet); } private void setXmlPath() { try { setXmlPath( LoginContext.server.getWebXMLRoot(LoginContext.sessionKey)); } catch (NoAccessException e) { log.error("Could not get xmlpath", e); } } public abstract boolean doPublish(Article article, Stylesheet stylesheet) throws TransformerException, IOException; public Stylesheet getStylesheet(Article article, PublishingMediaEnum publishingMedia) { try { return LoginContext.server.getStylesheet(publishingMedia, article.getSection(), LoginContext.sessionKey); } catch (NoAccessException e) { log.error("Could not get stylesheet", e); return null; } } protected Properties getOutputProperties(){ Properties p = new Properties(); p.setProperty(OutputKeys.ENCODING, encoding.getName()); p.setProperty(OutputKeys.METHOD, outputMethod.name()); return p; } public String getXmlPath() { return xmlPath; } public void setXmlPath(String xmlPath) { this.xmlPath = xmlPath; } public File getFile() { return file; } public void setEncoding(OutputEncoding encoding) { this.encoding = encoding; } public OutputEncoding getEncoding() { return encoding; } public OutputMethod getOutputMethod() { return outputMethod; } public void setOutputMethod(OutputMethod outputMethod) { this.outputMethod = outputMethod; } public void setMode(CRLFFilterOutputStream.Mode mode) { this.mode = mode; } public CRLFFilterOutputStream.Mode getMode() { return mode; } public enum OutputEncoding{ utf8("utf-8"), Cp1252("Cp1252"), MacRoman("MacRoman"), currentPlatform(System.getProperty("file.encoding")); private String name; OutputEncoding(String name){ this.name = name; } public String getName(){ return this.name; } } public enum OutputMethod{ text, html, xml } }