/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.hp.hpl.jena.sparql.util;

import java.lang.reflect.Field ;
import java.util.ArrayList ;
import java.util.Iterator ;
import java.util.List ;

import org.apache.jena.atlas.io.IndentedWriter ;

/** Manage version information for subsystems */
public class Version
{
    private List<Class< ? >> classes = new ArrayList<Class< ? >>() ; 
    public void addClass(Class< ? > c)
    {
        if ( ! classes.contains(c) ) 
            classes.add(c) ;
    }
    
    private static String[] fields = { /*"NAME",*/ "VERSION", "BUILD_DATE" } ;

    public void print()
    {    
        for ( Iterator<Class<?>> iter = classes.iterator() ; iter.hasNext() ; )
        {
            Class<?> c = iter.next();
            String x = Utils.classShortName(c) ;
            fields(x, c) ;    
        }
    }
    
    private static void fields(String prefix, Class< ? > cls)
    {
        for (int i=0; i < fields.length; i++)
            printField(IndentedWriter.stdout, prefix, fields[i], cls) ;
    }
    
    private static String field(String fieldName, Class< ? > cls)
    {
        try
        {
            Field f = cls.getDeclaredField(fieldName) ;
            return f.get(null).toString() ;
        } catch (IllegalArgumentException ex)
        {
            ex.printStackTrace();
        } catch (IllegalAccessException ex)
        {
            ex.printStackTrace();
        } catch (SecurityException ex)
        {
            ex.printStackTrace();
        } catch (NoSuchFieldException ex)
        {
            ex.printStackTrace();
        }
        return "<error>" ;
    }
    
    private static void printField(IndentedWriter out, String prefix, String fieldName, Class< ? > cls)
    {
        out.print(prefix) ;
        out.print(": ") ;
        out.pad(12) ;
        out.print(fieldName) ;
        out.print(": ") ;
        out.print(field(fieldName, cls)) ;
        out.println() ;
        out.flush();
    }
}

@@ -953,16 +953,68 @@
 List ;%0A%0A
+import org.apache.jena.atlas.io.IndentedLineBuffer;%0A
 import o
@@ -1197,16 +1197,105 @@
 %3E%3E() ; %0A
+    %0A    /**%0A     * Add a class to the version information%0A     * @param c Class%0A     */%0A
     publ
@@ -1489,16 +1489,109 @@
 E%22 %7D ;%0A%0A
+    /**%0A     * Prints version information for all registered classes to Standard Out%0A     */%0A
     publ
@@ -1812,16 +1812,39 @@
  fields(
+IndentedWriter.stdout, 
 x, c) ; 
@@ -1860,32 +1860,520 @@
    %7D%0A    %7D%0A    %0A
+    /**%0A     * Gets version information for all registered classes as a string%0A     */%0A    @Override%0A    public String toString() %7B%0A        IndentedLineBuffer buffer = new IndentedLineBuffer(false);%0A        %0A        for ( Iterator%3CClass%3C?%3E%3E iter = classes.iterator() ; iter.hasNext() ; )%0A        %7B%0A            Class%3C?%3E c = iter.next();%0A            String x = Utils.classShortName(c) ;%0A            fields(buffer, x, c) ;    %0A        %7D%0A        %0A        return buffer.asString();%0A    %7D%0A    %0A
     private stat
@@ -2387,16 +2387,39 @@
  fields(
+IndentedWriter writer, 
 String p
@@ -2516,37 +2516,22 @@
 ntField(
-IndentedWriter.stdout
+writer
 , prefix

Provide a usable toString() method on Version