All Downloads are FREE. Search and download functionalities are using the official Maven repository.

word.w2004.Document2004 Maven / Gradle / Ivy

The newest version!
package word.w2004;

import word.api.interfaces.IBody;
import word.api.interfaces.IDocument;
import word.api.interfaces.IElement;
import word.api.interfaces.IFooter;
import word.api.interfaces.IHeader;

/**
 * This is the main class in this API. It represents the MS Word document.
 * @author leonardo_correa
 *
 */
public class Document2004 implements IDocument, IElement{

    private boolean hasBeenCalledBefore = false; // if getContent has already been called, I cached the result for future invocations

    private StringBuilder txt = new StringBuilder();
    private IBody body = new Body2004();
    private boolean isLandscape = false;

    //Document properties 
    private String title = "Java2word title";
    private String subject = "Created by Java2word library";
    private String keywords = "java2word, word document";
    private String description = "";
    private String category = "";
    private String author = "Leonardo Correa";
    private String lastAuthor = "Leonardo Correa";
    private String manager = "Leonardo Correa";
    private String company = "Java2word, coding for fun!";

    private String encoding = Encoding.UTF_8.getValue();

    public Document2004() {
    	
	}
    
    public Document2004(String xml) {
    	txt.append(xml);
    	hasBeenCalledBefore = true; 
    }
    
    @Override
    public String getContent() {
        if(hasBeenCalledBefore ){
            return txt.toString();
        }else{
            hasBeenCalledBefore = true;
        }
        txt.append(this.getUri());
        txt.append(getDocumentHead());

        txt.append(this.getBody().getContent());

        txt.append("\n");

        String finalString = setUpPageOrientation(txt.toString());

        return finalString;
    }

    /**
     * Returns the Document head that contains: DocumentProperties, fonts and styles
     * @return
     */
    private Object getDocumentHead() {
        String docHead = DocumentHead;

        // replace properties place holder
        docHead = docHead.replace("{title}", title);
        docHead = docHead.replace("{subject}", subject);
        docHead = docHead.replace("{keywords}", keywords);
        docHead = docHead.replace("{description}", description);
        docHead = docHead.replace("{category}", category);
        docHead = docHead.replace("{author}", author);
        docHead = docHead.replace("{lastAuthor}", lastAuthor);
        docHead = docHead.replace("{manager}", manager);
        docHead = docHead.replace("{company}", company);

        return docHead;
    }

    private String setUpPageOrientation(String txt) {
        if(isLandscape) {
            String orientation = "    \n"
                + "      \n"
                + "      \n"
                + "      \n" + "    ";
            txt = txt.replace("", orientation + "\n");
        }
        return txt;
    }

    @Override
    public void setPageOrientationLandscape() {
        isLandscape = true;
    }

    //### Getters and Setters
    @Override
    public IBody getBody() {
        return body;
    }
    @Override
    public IFooter getFooter() {//forward it to the body
        return this.getBody().getFooter();
    }
    @Override
    public IHeader getHeader() {
        return this.getBody().getHeader(); //forward it to the body
    }

    /**
     * This is an alias to 'getBody().addEle'
     */
    @Override
    public void addEle(IElement e) {
        this.getBody().addEle(e);
    }

    /**
     * This is an alias to 'getBody().addEle'
     */
    @Override
    public void addEle(String str) {
        this.getBody().addEle(str);
    }

    @Override
    public String toString() {
        return this.getContent();
    }

    @Override
    public Document2004 title(String title) {
        this.title = title;
        return this;
    }
    @Override
    public Document2004 subject(String subject) {
        this.subject = subject;
        return this;
    }
    @Override
    public Document2004 keywords(String keywords) {
        this.keywords = keywords;
        return this;
    }
    @Override
    public Document2004 description(String description) {
        this.description = description;
        return this;
    }
    @Override
    public Document2004 category(String category) {
        this.category = category;
        return this;
    }
    @Override
    public Document2004 author(String author) {
        this.author = author;
        return this;
    }
    @Override
    public Document2004 lastAuthor(String lastAuthor) {
        this.lastAuthor = lastAuthor;
        return this;
    }
    @Override
    public Document2004 manager(String manager) {
        this.manager = manager;
        return this;
    }
    @Override
    public Document2004 company(String company) {
        this.company = company;
        return this;
    }

    @Override
    public Document2004 encoding(Encoding encoding) {
        this.encoding = encoding.getValue();
        return this;
    }
    
    @Override
    public Document2004 encoding(String encoding) {
        this.encoding = encoding;
        return this;
    }

    //######### Constants, variables #######

    @Override
    public String getUri() {
        String uri = " "
            + " "
            + " "
            + "  ";
        return uri;
    }

    public static final String DocumentHead =
        "     "
      + "        {title} \n"
      + "        {subject} \n"
      + "        {keywords} \n"
      + "        {description} \n"
      + "        {category} \n"
      + "        {author} "
      + "        {lastAuthor} "
      + "        {manager} \n"
      + "        {company} \n"
      + "        1 "
      + "        1 "
      + "        2010-07-16T07:18:00Z "
      + "        2010-07-16T07:22:00Z "
      + "        1 "
      + "        0 "
      + "        0 "
      + "        1 \n"
      + "        1 "
      + "        1 "
      + "        0 "
      + "        1 "
      + "     "
      + "     "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "         "
      + "     "
      + "     "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                     "
      + "                     "
      + "                     "
      + "                     "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "                 "
      + "             "
      + "         "
      //Hyperlink Style
      + "        j"
      + "             "
      + "             "
      + "             "
      + "             "
      + "                 "
      + "                 "
      + "             "
      + "         "
      //HEADER_N_FOOTER_STYLE
      +"         "
      +"             "
      +"         "

      + "         "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"                 "
      +"                     "
      +"                     "
      +"                 "
      +"             "
      +"             "
      +"                 "
      +"             "
      +"         "
      +"         "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"                 "
      +"                 "
      +"             "
      +"         "
      +"         "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"                 "
      +"                     "
      +"                     "
      +"                 "
      +"             "
      +"             "
      +"                 "
      +"             "
      +"         "
      +"         "
      +"             "
      +"             "
      +"             "
      +"             "
      +"             "
      +"                 "
      +"                 "
      +"             "
      +"         "
      +"         "
      +"             "
      +"             "
      +"             "
      +"             "
      +"         "

      + "     "
      + "     "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "         "
      + "             "
      + "             "
      + "             "
      + "             "
      + "             "
      + "         "
      + "         "
      + "             "
      + "         "
      + "     ";


    public enum Encoding{
        UTF_8("UTF-8"), 
        ISO8859_1("ISO8859-1"),
        /**
         * This is suitable for cirrylic text
         */
        WINDOWS_1251("windows-1251");

        private String value;

        Encoding(String value){
            this.value = value;
        }

        public String getValue(){
            return value;
        }

    };


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy