ContentManager.java



package xmlshare;

import java.io.*;
import java.util.ArrayList;
public class ContentManager {

 /** Output File */
    private File outFile;

 /** File Name List */
    private String[] names;

 /** open dir */
    private String prefix;

 /** DTD info */
    private String[]  dtd = {
            "<!DOCTYPE list [",
            "<!ELEMENT list (file+)>",
            "<!ELEMENT file (name)>",
            "<!ELEMENT name (#PCDATA)>",
            " ]>" };
    private String  rootBegin = "<list>";
    private String  rootEnd = "</list>";
    private String[]  prepattern = {
            "<file>", "<name>" };
    private String[]  epipattern = {
            "</name>", "</file>" };

 /** constructor */
    public ContentManager() {
        try {
             prefix = "public" + File.separator;
             outFile = new File( prefix + "list.xml" );
             File dir = new File( prefix );
             names = dir.list();
             remakeList();
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }

 /** output info */
    protected void remakeList() {
        try {
            PrintWriter out = new PrintWriter(
                                 new FileWriter( outFile ) );
            for( int i=0; i<dtd.length; i++ ) {
                out.println( dtd[i] );
            }
            out.println( rootBegin );
            boolean listUp = false;
            for( int i=0; i<names.length; i++ ) {
                if( names[i].endsWith( ".xml" ) ||
                    names[i].endsWith( ".XML" ) ) {
                    for( int j=0; j<prepattern.length; j++ ) {
                       out.println( prepattern[j] );
                    }
                    out.println( names[i] );
                    for( int j=0; j<epipattern.length; j++ ) {
                       out.println( epipattern[j] );
                    }
                }
                if( names[i].equals( "list.xml" ) )
                    listUp = true;
            }
            if( !listUp ) {
                for( int j=0; j<prepattern.length; j++ ) {
                   out.println( prepattern[j] );
                }
                out.println( "list.xml" );
                for( int j=0; j<epipattern.length; j++ ) {
                   out.println( epipattern[j] );
                }
            }
            out.println( rootEnd );
            out.flush();
            out.close();
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }

 /** start here(for test) */
    public static void main( String argv[] ) {

        ContentManager test = new ContentManager();
    }
}