/************************************************************************
*
* LaTeXDocument.java
*
* Copyright: 2002-2015 by Henrik Just
*
* This file is part of Writer2LaTeX.
*
* Writer2LaTeX 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.
*
* Writer2LaTeX is 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 Writer2LaTeX. If not, see
Class representing a LaTeX document.
* */ public class LaTeXDocument implements OutputFile { private static final String FILE_EXTENSION = ".tex"; private String sName; private boolean bIsMaster; private String sEncoding = "ASCII"; private int nWrap; private LaTeXDocumentPortion contents; /** *Constructs a new LaTeX Document.
* *This new document is empty. Document data must added to the preamble and * the body using appropriate methods.
* * @param sName The name of theLaTeXDocument
.
* @param nWrap Lines should be wrapped after this position
* @param bIsMaster true if this is a master document
*/
public LaTeXDocument(String sName,int nWrap,boolean bIsMaster) {
this.nWrap = nWrap;
this.sName = trimDocumentName(sName);
this.bIsMaster = bIsMaster;
contents = new LaTeXDocumentPortion(true);
}
/**
* Returns the Document
name with no file extension.
Document
name with no file extension.
*/
public String getName() {
return sName;
}
/**
* Returns the Document
name with file extension.
Document
name with file extension.
*/
public String getFileName() {
return new String(sName + FILE_EXTENSION);
}
public String getMIMEType() {
return MIMETypes.LATEX;
}
public boolean isMasterDocument() {
return bIsMaster;
}
public boolean containsMath() {
// We don't use this information currently
return true;
}
/**
* Writes out the Document
content to the specified
* OutputStream
.
This method may not be thread-safe. * Implementations may or may not synchronize this * method. User code (i.e. caller) must make sure that * calls to this method are thread-safe.
* * @param osOutputStream
to write out the
* Document
content.
*
* @throws IOException If any I/O error occurs.
*/
public void write(OutputStream os) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(os,sEncoding);
contents.write(osw,nWrap,"\n");
osw.flush();
osw.close();
}
/**
* Set the output encoding to use when writing the document.
*/ public void setEncoding(String sEncoding) { this.sEncoding = sEncoding; } /** *Returns the LaTeXDocumentPortion
, that contains the
* contents of the document.
LaTeXDocumentPortion
.
*/
public LaTeXDocumentPortion getContents(){
return contents;
}
/*
* Utility method to make sure the document name is stripped of any file
* extensions before use.
*/
private String trimDocumentName(String name) {
String temp = name.toLowerCase();
if (temp.endsWith(FILE_EXTENSION)) {
// strip the extension
int nlen = name.length();
int endIndex = nlen - FILE_EXTENSION.length();
name = name.substring(0,endIndex);
}
return name;
}
}