/* 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.Stylesheet; import org.pegadi.publisher.io.CRLFFilterOutputStream; import org.w3c.dom.Document; import org.w3c.dom.Element; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.FileNotFoundException; import java.io.FileOutputStream; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class StylesheetPublisher extends AbstractPublisher{ @Override public boolean doPublish(Article article, Stylesheet stylesheet) throws TransformerException, FileNotFoundException { setOutputMethod(OutputMethod.text); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource(getXmlPath() + "/" + stylesheet.getStylesheetURL())); transformer.setOutputProperties(getOutputProperties()); transformer.transform(new DOMSource(article.getDocument()), new StreamResult(new CRLFFilterOutputStream(new FileOutputStream(getFile()), getMode()))); log.info("Transformation done"); return true; } /** * Inserts an element in the article DOM-tree describing the encoding to be read by indesign, and serializes the article * @param article The article * @param encoding The string describing the encoding (on the form ENCODING-PLATFORM, like ANSI-MAC or UNICODE-WIN) */ public void setArticleIndesignEncodingTag(Article article, String encoding) { if(article.parseText()) { Document doc = article.getDocument(); Element head = (Element)doc.getElementsByTagName("head").item(0); Element encodingTagNode = doc.createElement("encodingTag"); head.appendChild(encodingTagNode); encodingTagNode.appendChild(doc.createTextNode(encoding)); if(!article.serialize()) { log.error("error setting the encoding tag element in the article DOM-tree."); } } } }