1

我将 HTML 文件和一个 XSLT 文件作为输入并生成 HTML 输出,但在我的文件夹中有多个 HTML 文件,我必须将它们中的每一个作为输入并生成相应的输出文件,而 XSLT 输入文件每次都保持不变. 目前在我的代码中,我每次都重复代码块以获取输入 HTML 文件。取而代之的是,我想遍历文件夹中的所有 HTML 文件,并将它们一一作为输入文件以生成输出。在我当前的代码中,文件名也是固定的,part_1.html但它可能会有所不同,在这种情况下,此代码将不起作用,这会产生问题。任何人都可以在这件事上帮忙:谢谢!

当前 Java 代码(两个文件的示例):

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);
    }
}
4

4 回答 4

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) {
    ...
}
于 2012-08-18T10:39:28.737 回答
1

你必须实现类似的东西

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");

          }
        }
      }       
    }
  }
}
于 2012-08-18T09:39:46.330 回答
1

首先,您应该编写一个将inXMLinXSL和作为参数的方法。outTXTrenamedFile

然后,使用最终采用 alist()的类的方法,您可以遍历要转换的文件。FileFilenameFilter

这是一个示例FilenameFilter

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

问候

于 2012-08-18T09:44:23.237 回答
1

使用DirectoryScanner这将使您的工作更轻松

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

于 2012-08-18T09:57:12.970 回答