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

net.anotheria.asg.generator.parser.XMLPreprocessor Maven / Gradle / Ivy

package net.anotheria.asg.generator.parser;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import net.anotheria.asg.generator.util.IncludedDocuments;
import net.anotheria.util.IOUtils;
import net.anotheria.util.StringUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * This class preprocess an xml file. Its mainly used to put together a splitted xml file.
 *
 * @author another
 * @version $Id: $Id
 */
@SuppressWarnings("ALL")
public final class XMLPreprocessor {

    /**
     * Include commands 'tag'.
     */
    private static final String INCLUDE_COMMAND = "@include:";

    /**
     * Loads a file from the disk. Include marks are replaced with the content of the included files.
     *
     * @param f a {@link java.io.File} object.
     * @throws java.io.IOException if any.
     * @param includedDocuments a {@link net.anotheria.asg.generator.util.IncludedDocuments} object.
     * @return a {@link java.lang.String} object.
     */
    public static String loadFile(File f, IncludedDocuments includedDocuments) throws IOException {
        if (!f.exists())
            throw new IOException("File doesn't exists: "+f.getAbsolutePath());
        int buffLineNumber = 0;
        String content = IOUtils.readFileAtOnceAsString(f.getAbsolutePath());

        boolean includeDocumentIsNull = true;
        if (includedDocuments != null){
            includeDocumentIsNull = false;
            if ( !includedDocuments.isListEmpty()){
                buffLineNumber = includedDocuments.getLastInsertLine();
            }
        }


        List tags = StringUtils.extractTags(content, '<', '>');
        for (String t : tags){
            if (!t.startsWith("