001/* 002 * Copyright 2017 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.plugins.contenttypeseditor; 017 018import java.io.IOException; 019import java.io.OutputStream; 020 021import org.apache.avalon.framework.activity.Initializable; 022import org.apache.cocoon.serialization.Serializer; 023import org.xml.sax.SAXException; 024import org.xml.sax.ext.DefaultHandler2; 025 026import net.sourceforge.plantuml.SourceStringReader; 027 028/** 029 * Serialize PlantUML 030 */ 031public class PlantUMLSerializer extends DefaultHandler2 implements Serializer, Initializable 032{ 033 /** Planuml picture size **/ 034 private static final String PLANTUML_LIMIT_SIZE = "PLANTUML_LIMIT_SIZE"; 035 036 /** Buffer to store plantuml script */ 037 private StringBuilder _buffer; 038 039 /** OutputStream where the requested resource should be serialized */ 040 private OutputStream _out; 041 042 public void initialize() throws Exception 043 { 044 System.setProperty(PLANTUML_LIMIT_SIZE, String.valueOf(Integer.MAX_VALUE)); 045 } 046 047 @Override 048 public void startDocument() throws SAXException 049 { 050 _buffer = new StringBuilder(); 051 } 052 053 @Override 054 public void endDocument() throws SAXException 055 { 056 SourceStringReader reader = new SourceStringReader(_buffer.toString()); 057 try 058 { 059 reader.outputImage(_out); 060 } 061 catch (IOException e) 062 { 063 throw new RuntimeException("Unable to export content types diagram", e); 064 } 065 } 066 067 @Override 068 public void characters(char[] ch, int start, int length) throws SAXException 069 { 070 String txt = new String(ch, start, length); 071 _buffer.append(txt); 072 } 073 074 public void setOutputStream(OutputStream out) throws IOException 075 { 076 _out = out; 077 } 078 079 public String getMimeType() 080 { 081 return "image/png"; 082 } 083 084 public boolean shouldSetContentLength() 085 { 086 return false; 087 } 088 089}