Coverage Report - org.apache.any23.cli.VocabPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
VocabPrinter
0%
0/35
0%
0/2
4
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.cli;
 19  
 
 20  
 import org.apache.any23.vocab.RDFSchemaUtils;
 21  
 import org.apache.commons.cli.CommandLine;
 22  
 import org.apache.commons.cli.CommandLineParser;
 23  
 import org.apache.commons.cli.HelpFormatter;
 24  
 import org.apache.commons.cli.Option;
 25  
 import org.apache.commons.cli.Options;
 26  
 import org.apache.commons.cli.PosixParser;
 27  
 
 28  
 import java.io.BufferedOutputStream;
 29  
 import java.io.IOException;
 30  
 import java.util.Arrays;
 31  
 
 32  
 /**
 33  
  * Prints out the vocabulary <i>RDFSchema</i> as <i>NQuads</i>.
 34  
  *
 35  
  * @author Michele Mostarda (mostarda@fbk.eu)
 36  
  */
 37  
 @ToolRunner.Description("Prints out the RDF Schema of the vocabularies used by Any23.")
 38  0
 public class VocabPrinter implements Tool {
 39  
 
 40  
     public static void main(String[] args) throws IOException {
 41  0
         System.exit( new VocabPrinter().run(args) );
 42  0
     }
 43  
 
 44  
     public int run(String[] args) {
 45  0
         final CommandLineParser parser = new PosixParser();
 46  
         final CommandLine commandLine;
 47  
         final RDFSchemaUtils.VocabularyFormat format;
 48  
         try {
 49  0
             final Options options = new Options();
 50  0
             options.addOption(
 51  
                     new Option("h", "help", false, "Print this help.")
 52  
             );
 53  0
             options.addOption(
 54  
                     new Option(
 55  
                         "f", "format",
 56  
                         true,
 57  
                         "Vocabulary output format, supported values are: " +
 58  
                         Arrays.toString(RDFSchemaUtils.VocabularyFormat.values())
 59  
                     )
 60  
             );
 61  0
             commandLine = parser.parse(options, args);
 62  0
             if (commandLine.hasOption("h")) {
 63  0
                 printHelp(options);
 64  0
                 return 0;
 65  
             }
 66  
             try {
 67  0
                 format = RDFSchemaUtils.VocabularyFormat.valueOf(
 68  
                         commandLine.getOptionValue("f", RDFSchemaUtils.VocabularyFormat.NQuads.name())
 69  
                 );
 70  0
             } catch (IllegalArgumentException iae) {
 71  0
                 throw new IllegalArgumentException("Unknown format [" + commandLine.getOptionValue("f") + "'");
 72  0
             }
 73  0
         } catch (Exception e) {
 74  0
             e.printStackTrace(System.err);
 75  0
             return 1;
 76  0
         }
 77  
 
 78  0
         final BufferedOutputStream bos = new BufferedOutputStream(System.out);
 79  
         try {
 80  0
             RDFSchemaUtils.serializeVocabularies(format, System.out);
 81  0
         } catch (Exception e) {
 82  0
             e.printStackTrace(System.err);
 83  0
             return 1;
 84  
         } finally {
 85  0
             try {
 86  0
                 bos.flush();
 87  0
             } catch (IOException ioe) {
 88  0
                 ioe.printStackTrace(System.err);
 89  0
             }
 90  0
             System.out.println();
 91  0
         }
 92  0
         return 0;
 93  
     }
 94  
 
 95  
     private void printHelp(Options options) {
 96  0
         HelpFormatter formatter = new HelpFormatter();
 97  0
         formatter.printHelp(this.getClass().getSimpleName(), options, true);
 98  0
     }
 99  
 
 100  
 }