net.sourceforge.plantuml.ftp.FtpConnexion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml Show documentation
Show all versions of plantuml Show documentation
PlantUML is a component that allows to quickly write :
* sequence diagram,
* use case diagram,
* class diagram,
* activity diagram,
* component diagram,
* state diagram
* object diagram
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
* (C) Copyright 2009-2013, Arnaud Roques
*
* Project Info: http://plantuml.sourceforge.net
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* Original Author: Arnaud Roques
*
* Revision $Revision: 3830 $
*
*/
package net.sourceforge.plantuml.ftp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.FileFormatOption;
import net.sourceforge.plantuml.SourceStringReader;
public class FtpConnexion {
private final String user;
private final Map incoming = new HashMap();
private final Map outgoing = new HashMap();
public FtpConnexion(String user) {
this.user = user;
}
public synchronized void addIncoming(String fileName, String data) {
if (fileName.startsWith("/")) {
throw new IllegalArgumentException();
}
incoming.put(fileName, data);
}
public void removeOutgoing(String fileName) {
outgoing.remove(fileName);
}
public synchronized Collection getFiles() {
final List result = new ArrayList(incoming.keySet());
result.addAll(outgoing.keySet());
return Collections.unmodifiableCollection(result);
}
public synchronized byte[] getData(String fileName) {
if (fileName.startsWith("/")) {
throw new IllegalArgumentException();
}
final String data = incoming.get(fileName);
if (data != null) {
return data.getBytes();
}
final byte data2[] = outgoing.get(fileName);
if (data2 != null) {
return data2;
}
return new byte[0];
}
public synchronized int getSize(String fileName) {
if (fileName.startsWith("/")) {
throw new IllegalArgumentException();
}
final String data = incoming.get(fileName);
if (data != null) {
return data.length();
}
final byte data2[] = outgoing.get(fileName);
if (data2 != null) {
return data2.length;
}
return 0;
}
public void processImage(String fileName) throws IOException {
if (fileName.startsWith("/")) {
throw new IllegalArgumentException();
}
final SourceStringReader sourceStringReader = new SourceStringReader(incoming.get(fileName));
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final FileFormat format = FileFormat.PNG;
final String ok = sourceStringReader.generateImage(baos, new FileFormatOption(format));
final String pngFileName = format.changeName(fileName, 0);
final String errorFileName = pngFileName.substring(0, pngFileName.length() - 4) + ".err";
outgoing.remove(pngFileName);
outgoing.remove(errorFileName);
if (ok != null) {
synchronized (this) {
outgoing.put(pngFileName, baos.toByteArray());
if (ok.startsWith("(Error)")) {
final ByteArrayOutputStream errBaos = new ByteArrayOutputStream();
sourceStringReader.generateImage(errBaos, new FileFormatOption(FileFormat.ATXT));
errBaos.close();
outgoing.put(errorFileName, errBaos.toByteArray());
}
}
}
}
public synchronized void delete(String fileName) {
incoming.remove(fileName);
outgoing.remove(fileName);
}
}