de.fhg.iais.asc.viewspreview.ViewXmlScanner.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.viewspreview.ViewXmlScanner.java

Source

package de.fhg.iais.asc.viewspreview;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * 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.                                             *
 ******************************************************************************/

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;
import org.jdom.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.fhg.iais.asc.commons.AscConfiguration;
import de.fhg.iais.asc.workflow.AbstractScan;
import de.fhg.iais.commons.dbc.DbcException;
import de.fhg.iais.cortex.model.aip.util.XmlProcessor;

public class ViewXmlScanner extends AbstractScan {
    private static final Logger LOGGER = LoggerFactory.getLogger(ViewXmlScanner.class);

    private static final int MAXVIEWSINONEFILE = 500;
    private final ViewExtractor viewextractor;
    private int filecounter = 0;
    private FileWriter fileWriter = null;

    ViewXmlScanner(AscConfiguration config, String target) { // package protected on purpose
        super(config);
        this.viewextractor = new ViewExtractor();
        this.target = target;
    }

    private FileWriter createFile() throws IOException {
        final FileWriter result = new FileWriter(
                new File(this.target + File.separator + "views" + this.filecounter + ".html"), false);
        result.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
                + "\"http://www.w3.org/TR/html4/loose.dtd\">");
        result.write("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
                + "<title>DDB-Views (Demo)</title></head><body>");
        return result;
    }

    private void closeFile(FileWriter filewriter) throws IOException {
        if (this.fileWriter != null) {
            filewriter.write("</body></html>");
            filewriter.close();
            this.filecounter++;
        }
    }

    private FileWriter closeAndCreateFile(FileWriter filetoclose) throws IOException {
        closeFile(filetoclose);
        return createFile();
    }

    @Override
    public boolean nextFile(File file) {
        String xml;
        try {
            xml = FileUtils.readFileToString(file, Charsets.UTF_8);
        } catch (IOException e) {
            throw new DbcException(e);
        }
        Document xdoc = XmlProcessor.buildDocumentFrom(xml);
        String str = this.viewextractor.handle(xdoc);
        try {

            if (this.fileWriter == null) {
                this.fileWriter = createFile();
            }

            if (this.counter >= MAXVIEWSINONEFILE) {
                this.fileWriter = closeAndCreateFile(this.fileWriter);
                this.counter = 0;
            }

            this.fileWriter.write("<hr>");
            this.fileWriter.write("<font size=\"-1\">");
            this.fileWriter.write(file.getName());
            this.fileWriter.write("</font>");
            this.fileWriter.write(str);

            this.counter++;

        } catch (IOException e) {
            LOGGER.error(e.getMessage());
        }
        return false;
    }

    public void closeFile() throws IOException {
        closeFile(this.fileWriter);
    }
}