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

com.formkiq.vision.main.Application Maven / Gradle / Ivy

/*
 * Copyright (C) 2018 FormKiQ Inc.
 *
 * 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.formkiq.vision.main;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.formkiq.vision.crafter.DocumentCrafter;
import com.formkiq.vision.document.Document;
import com.formkiq.vision.document.DocumentSource;
import com.formkiq.vision.pdf.PDDocumentSource;
import com.formkiq.vision.util.JSONBuilder;

/**
 * 
 * Main class.
 *
 */
public final class Application {

	/**
	 * private constructor.
	 */
	private Application() {
		// private constructor.
	}
	
	/**
	 * Run Vision Document Conversion.
	 * 
	 * @param args String[]
	 * @throws IOException IOException 
	 */
	public static void main(final String[] args) throws IOException {

		if (args.length > 0) {

			System.out.println(toJSON(args[0], new FileInputStream(args[0])));

		} else {
			System.err.println("Usage ");
			System.exit(1);
		}
	}

	/**
	 * Converts File to JSON.
	 * 
	 * @param filename {@link String}
	 * @param is {@link InputStream}
	 * @return {@link String}
	 * @throws UnsupportedOperationException Unsupported File Operation
	 * @throws IOException IOException 
	 */
	public static String toJSON(final String filename, final InputStream is)
			throws UnsupportedOperationException, IOException {
		
		DocumentSource document = loadSource(filename, is);
		
		try {
			
			Document doc = new DocumentCrafter().buildDocument(document);
			return new JSONBuilder().toJSON(doc);
			
		} finally {

			if (document != null) {
				document.close();
			}
		}
	}
	
	/**
	 * Load {@link DocumentSource}.
	 * 
	 * @param filename {@link String}
	 * @param is       {@link InputStream}
	 * @return {@link DocumentSource}
	 * @throws IOException IOException
	 * @throws UnsupportedOperationException Unsupported File Format
	 */
	private static DocumentSource loadSource(final String filename, final InputStream is)
			throws IOException, UnsupportedOperationException {
		
		if (filename.endsWith(".pdf")) {	
			return new PDDocumentSource(is);
		}
		
		throw new UnsupportedOperationException("Unsupported file format");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy