org.araqne.docxcod.test.DocxTest.java Source code

Java tutorial

Introduction

Here is the source code for org.araqne.docxcod.test.DocxTest.java

Source

/*
 * Copyright 2013 Future Systems
 * 
 * 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.araqne.docxcod.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.araqne.docxcod.impl.AbtrTableDirectiveParser;
import org.araqne.docxcod.impl.DirectivePlacerProcessor;
import org.araqne.docxcod.impl.ChartDirectiveParser;
import org.araqne.docxcod.impl.FreeMarkerRunner;
import org.araqne.docxcod.impl.MagicNodeUnwrapper;
import org.araqne.docxcod.impl.MergeFieldParser;
import org.araqne.docxcod.impl.OOXMLPackage;
import org.araqne.docxcod.impl.OOXMLProcessor;
import org.araqne.docxcod.util.JsonHelper;
import org.araqne.docxcod.util.ZipHelper;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DocxTest {
    private Logger logger = LoggerFactory.getLogger(getClass().getName());

    private TearDownHelper tearDownHelper = new TearDownHelper();

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
        tearDownHelper.tearDown();
    }

    @Test
    public void totalTest() throws IOException, JSONException {
        File targetDir = new File(".test/_totalTest");
        File saveFile = new File(".test/totalTest-save.docx");
        saveFile.delete();
        targetDir.mkdirs();
        // tearDownHelper.add(targetDir);

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/totalTest.docx"), targetDir);
        docx.setOutputStream(new FileOutputStream(saveFile));

        InputStreamReader inputReader = new InputStreamReader(getClass().getResourceAsStream("/totalTest.in"),
                Charset.forName("utf-8"));
        JSONTokener tokener = new JSONTokener(inputReader);
        Map<String, Object> rootMap = JsonHelper.parse((JSONObject) tokener.nextValue());

        docx.apply(new MergeFieldParser(), rootMap);
        docx.apply(new DirectivePlacerProcessor(), rootMap);
        docx.apply(new ChartDirectiveParser(), rootMap);
        docx.apply(new MagicNodeUnwrapper("word/document.xml"), rootMap);
        docx.apply(new FreeMarkerRunner("word/document.xml"), rootMap);

        docx.setDebug(true);
        docx.close();
        // tearDownHelper.add(saveFile);
    }

    @Test
    public void arbitraryTableTest() throws IOException, JSONException {
        File targetDir = new File(".test/_abtrTableTest");
        File saveFile = new File(".test/abtrTableTest-save.docx");
        saveFile.delete();
        targetDir.mkdirs();
        // tearDownHelper.add(targetDir);

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/abtrTableTest.docx"), targetDir);
        docx.setOutputStream(new FileOutputStream(saveFile));

        InputStreamReader inputReader = new InputStreamReader(getClass().getResourceAsStream("/abtrTableTest.in"),
                Charset.forName("utf-8"));
        JSONTokener tokener = new JSONTokener(inputReader);
        Map<String, Object> rootMap = JsonHelper.parse((JSONObject) tokener.nextValue());

        List<OOXMLProcessor> processors = new ArrayList<OOXMLProcessor>();
        docx.apply(new MergeFieldParser(), rootMap);
        docx.apply(new DirectivePlacerProcessor(), rootMap);
        docx.apply(new AbtrTableDirectiveParser(), rootMap);
        docx.apply(new ChartDirectiveParser(), rootMap);
        docx.apply(new MagicNodeUnwrapper("word/document.xml"), rootMap);
        docx.apply(new FreeMarkerRunner("word/document.xml"), rootMap);

        docx.close();
        // tearDownHelper.add(saveFile);
    }

    @Test
    public void chartTest() throws IOException, JSONException {
        File targetDir = new File(".test/_chartTest");
        File saveFile = new File(".test/chartTest-save.docx");
        targetDir.mkdirs();
        saveFile.delete();
        // tearDownHelper.add(targetDir);

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/chartTest.docx"), targetDir);
        docx.setOutputStream(new FileOutputStream(saveFile));

        InputStreamReader inputReader = new InputStreamReader(getClass().getResourceAsStream("/nestedListTest.in"));
        JSONTokener tokener = new JSONTokener(inputReader);
        Map<String, Object> rootMap = JsonHelper.parse((JSONObject) tokener.nextValue());

        List<OOXMLProcessor> processors = new ArrayList<OOXMLProcessor>();
        processors.add(new MergeFieldParser());
        processors.add(new DirectivePlacerProcessor());
        processors.add(new ChartDirectiveParser());
        //      processors.add(new ImgDirectiveParser());
        processors.add(new MagicNodeUnwrapper("word/document.xml"));
        processors.add(new FreeMarkerRunner("word/document.xml"));

        docx.apply(processors, rootMap);

        docx.setDebug(true);
        docx.close();
    }

    @Test
    public void mainTest() throws IOException, JSONException {
        File targetDir = new File(".test/mainTest");
        File saveFile = new File(".test/mainTest-save.docx");
        saveFile.delete();
        targetDir.mkdirs();

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/nestedList2.docx"), targetDir);
        docx.setOutputStream(new FileOutputStream(saveFile));
        docx.setDebug(true);

        InputStreamReader inputReader = new InputStreamReader(getClass().getResourceAsStream("/nestedListTest.in"));
        JSONTokener tokener = new JSONTokener(inputReader);
        Map<String, Object> rootMap = JsonHelper.parse((JSONObject) tokener.nextValue());

        List<OOXMLProcessor> processors = new ArrayList<OOXMLProcessor>();
        processors.add(new MergeFieldParser());
        processors.add(new DirectivePlacerProcessor());
        processors.add(new MagicNodeUnwrapper("word/document.xml"));
        processors.add(new FreeMarkerRunner("word/document.xml"));

        docx.apply(processors, rootMap);

        docx.setDebug(true);
        docx.close();
    }

    @Test
    public void saveTest() throws IOException {
        File targetDir = new File(".test/saveTest");
        File saveFile = new File(".test/saveTest.docx");
        saveFile.delete();
        targetDir.mkdirs();
        tearDownHelper.add(targetDir);

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/nestedList2.docx"), targetDir);
        docx.setOutputStream(new FileOutputStream(saveFile));

        tearDownHelper.add(saveFile);
        // diff word file

        docx.close();
    }

    @Test
    public void extractingTest() throws IOException {
        File targetDir = new File(".test/extractingTest");
        targetDir.mkdirs();
        tearDownHelper.add(targetDir);

        OOXMLPackage docx = new OOXMLPackage();
        docx.load(getClass().getResourceAsStream("/extractingTest.docx"), targetDir);

        String[] relPaths = { "", "[Content_Types].xml", "_rels", "_rels\\.rels", "customXml", "customXml\\_rels",
                "customXml\\_rels\\item1.xml.rels", "customXml\\item1.xml", "customXml\\itemProps1.xml", "docProps",
                "docProps\\app.xml", "docProps\\core.xml", "word", "word\\_rels", "word\\_rels\\document.xml.rels",
                "word\\charts", "word\\charts\\_rels", "word\\charts\\_rels\\chart1.xml.rels",
                "word\\charts\\chart1.xml", "word\\document.xml", "word\\embeddings",
                "word\\embeddings\\Microsoft_Excel_____1.xlsx", "word\\endnotes.xml", "word\\fontTable.xml",
                "word\\footnotes.xml", "word\\settings.xml", "word\\styles.xml", "word\\stylesWithEffects.xml",
                "word\\theme", "word\\theme\\theme1.xml", "word\\webSettings.xml" };

        ArrayList<File> extractedFiles = new ArrayList<File>();
        ZipHelper.getFilesRecursivelyIn(targetDir, extractedFiles);

        File[] files = new File[relPaths.length];
        for (int i = 0; i < relPaths.length; ++i) {
            files[i] = new File(targetDir, normalizeSeparator(relPaths[i]));
        }
        Arrays.sort(files);
        Object[] extractedArray = extractedFiles.toArray();
        Arrays.sort(extractedArray);

        assertTrue(Arrays.equals(extractedArray, files));

        docx.close();
    }

    private String normalizeSeparator(String string) {
        return string.replace("/", File.separator).replace("\\", File.separator);
    }

    @Test
    public void relsTest() {
    }
}