com.alefissak.parsers.PlanningFromFilePathParser.java Source code

Java tutorial

Introduction

Here is the source code for com.alefissak.parsers.PlanningFromFilePathParser.java

Source

/**
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2014 - Nabil Andriantomanga.
 *
 * 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 com.alefissak.parsers;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import com.alefissak.io.AlefissakFileNotFoundException;
import com.alefissak.util.StringUtil;

/**
 * @author Nabil
 */
public class PlanningFromFilePathParser extends PlanningParserImpl {

    protected String planningFilePath; // the path to the configuration file
    // that contains all plannings ...

    /**
     * Constructor of {@link com.alefissak.parsers.PlanningFromFilePathParser}
     * 
     * @param planningFilePath
     */
    public PlanningFromFilePathParser(String planningFilePath) {
        super();
        this.planningFilePath = planningFilePath;
    }

    /**
     * {@inheritDoc}
     * 
     * @return the document object
     * @throws AlefissakFileNotFoundException
     * @throws AlefissakParsingException
     */
    public Document getDocument() throws AlefissakFileNotFoundException, AlefissakParsingException {

        if (StringUtil.isEmpty(planningFilePath)) {

            throw new IllegalArgumentException("The configuration file name must be specified !");
        }

        if (!new java.io.File(planningFilePath).exists()) {

            throw new AlefissakFileNotFoundException();
        }

        Document document = null;

        try {

            document = new SAXReader().read(planningFilePath);

        } catch (Exception ex) {

            throw new AlefissakParsingException(ex);
        }

        return document;
    }

}