1

I'm taking HTML file and one XSLT file as input and generating HTML output but in my folder there are multiple HTML files and I've to take each of them as input and generate the corresponding output file while XSLT input file remains same every time. Currently in my code I'm repeating the code block every time to take the input HTML file. Instead of this I want to iterate over all the HTML files in the folder and take them as input file one by one to generate the output. In my current code file names are also fixed like part_1.html but it can vary and in that case this code won't work and this will create problem. Can anyone please help out in this matter: Thanking you!

Current Java code (Sample for two files):

public void tranformation() {
        // TODO Auto-generated method stub
        transform1();
        transform2();
    }
    public static void transform1(){
        String inXML = "C:/SCORM_CP/part_1.html";
        String inXSL = "C:/source/xslt/html_new.xsl";
        String outTXT = "C:/SCORM_CP/part1_copy_copy.html";
        String renamedFile = "C:/SCORM_CP/part_1.html";
        File oldfile =new File(outTXT);
        File newfile =new File(renamedFile);
        HTML_Convert hc = new HTML_Convert();
        try {
            hc.transform(inXML,inXSL,outTXT);
            } catch(TransformerConfigurationException e) {
            System.err.println("Invalid factory configuration");
            System.err.println(e);
            } catch(TransformerException e) {
            System.err.println("Error during transformation");
            System.err.println(e);
        }
        try{
            File file = new File(inXML);
            if(file.delete()){
                System.out.println(file.getName() + " is deleted!");
                }else{
                System.out.println("Delete operation is failed.");
            }
            }catch(Exception e){
            e.printStackTrace();
        }
        if(oldfile.renameTo(newfile)){
            System.out.println("Rename succesful");
            }else{
            System.out.println("Rename failed");
        }
    }
    public static void transform2(){
        String inXML = "C:/SCORM_CP/part_2.html";
        String inXSL = "C:/source/xslt/html_new.xsl";
        String outTXT = "C:/SCORM_CP/part2_copy_copy.html";
        String renamedFile = "C:/SCORM_CP/part_2.html";
        File oldfile =new File(outTXT);
        File newfile =new File(renamedFile);
        HTML_Convert hc = new HTML_Convert();
        try {
            hc.transform(inXML,inXSL,outTXT);
            } catch(TransformerConfigurationException e) {
            System.err.println("Invalid factory configuration");
            System.err.println(e);
            } catch(TransformerException e) {
            System.err.println("Error during transformation");
            System.err.println(e);
        }
        try{
            File file = new File(inXML);
            if(file.delete()){
                System.out.println(file.getName() + " is deleted!");
                }else{
                System.out.println("Delete operation is failed.");
            }
            }catch(Exception e){
            e.printStackTrace();
        }
        if(oldfile.renameTo(newfile)){
            System.out.println("Rename succesful");
            }else{
            System.out.println("Rename failed");
        }
    }
    public void transform(String inXML,String inXSL,String outTXT)
    throws TransformerConfigurationException,
    TransformerException {
        TransformerFactory factory = TransformerFactory.newInstance();
        StreamSource xslStream = new StreamSource(inXSL);
        Transformer transformer = factory.newTransformer(xslStream);
        transformer.setErrorListener(new MyErrorListener());
        StreamSource in = new StreamSource(inXML);
        StreamResult out = new StreamResult(outTXT);
        transformer.transform(in,out);
        System.out.println("The generated XML file is:" + outTXT);
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
RahulD
  • 709
  • 2
  • 16
  • 39

4 Answers4

2
File dir = new File("/path/to/dir");
File[] htmlFiles = dir.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
        return name.endsWith(".html");
    }
});
if (htmlFiles != null) for (File html: htmlFiles) {
    ...
}
jkee
  • 693
  • 1
  • 6
  • 5
1

You have to implement something like

public class Transformation {

  public static void main (String[] args){
    transformation(".", ".");
  }

  public static void transform(String inXML, String inXSL, String outTXT, String renamedFile){
    System.out.println(inXML);
    System.out.println(inXSL);
    System.out.println(outTXT);
    System.out.println(renamedFile);
  }

  public static void transformation(String inFolder, String outFolder){
    File infolder = new File(inFolder);
    File outfolder = new File(outFolder);
    if (infolder.isDirectory() && outfolder.isDirectory()){
      System.out.println("In " + infolder.getAbsolutePath());
      System.out.println("Out " + outfolder.getAbsolutePath());
      File[] listOfFiles = infolder.listFiles();
      String outPath = outfolder.getAbsolutePath();
      String inPath = infolder.getAbsolutePath();
      for (File f: listOfFiles) {
        if (f.isFile() ) {
          System.out.println("File " + f.getName());
          int indDot = f.getName().lastIndexOf(".");
          String name = f.getName().substring(0, indDot);
          String ext = f.getName().substring(indDot+1);
          if (ext != null && ext.equals("html")){
            transform(f.getAbsolutePath(), inPath+File.separator+name+".xsl", outPath+File.separator+name+".txt", outPath+File.separator+name+".html");

          }
        }
      }       
    }
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • Thanks for the answer.I tried this solution and getting the following output- In C:\Users\omsairam\workspace\XSLT\. Out C:\Users\omsairam\workspace\XSLT\. File .classpath File .project File part_1.html C:\Users\omsairam\workspace\XSLT\.\part_1.html C:\Users\omsairam\workspace\XSLT\.\part_1.xsl C:\Users\omsairam\workspace\XSLT\.\part_1.txt C:\Users\omsairam\workspace\XSLT\.\part_1.html File part_2.html C:\Users\omsairam\workspace\XSLT\.\part_2.html C:\Users\omsairam\workspace\XSLT\.\part_2.xsl C:\Users\omsairam\workspace\XSLT\.\part_2.txt C:\Users\omsairam\workspace\XSLT\.\part_2.html – RahulD Aug 18 '12 at 12:16
  • While I my XSLT(.xsl) input file should remain same in every case and it is located in a separate folder. Please can you modify the the code accordingly. Thanks again. – RahulD Aug 18 '12 at 12:19
  • use different `in` and `out` folders, use the same name and different extensions conventions. only `html` files processed. – Roman C Aug 18 '12 at 13:03
1

First you should write a method that take inXML, inXSL, outTXT and renamedFile as arguments.

Then, using the list() method of the File class, that eventually take a FilenameFilter, you may iterate over the files you want to transform.

Here is a sample of FilenameFilter :

FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.contains("part");
    }
};

Regards

Y__
  • 1,687
  • 2
  • 11
  • 23
1

use DirectoryScanner which will make your job easier

Example of usage:

   String[] includes = {"**\*.html"};
   ds.setIncludes(includes);
   ds.setBasedir(new File("test"));
   ds.setCaseSensitive(true);
   ds.scan();

   System.out.println("FILES:");
   String[] files = ds.getIncludedFiles();
   for (int i = 0; i < files.length; i++) {
     System.out.println(files[i]);
   }

http://plexus.codehaus.org/plexus-utils/apidocs/org/codehaus/plexus/util/DirectoryScanner.html

Byter
  • 1,132
  • 1
  • 7
  • 12