2012-08-18 3 views
1

HTML 파일과 하나의 XSLT 파일을 입력으로 사용하고 HTML 출력을 생성하지만 내 폴더에는 여러 HTML 파일이 있으며 각각을 입력으로 생성해야합니다. XSLT 입력 파일이 매번 동일 할 때 해당 출력 파일. 현재 내 코드에서 입력 HTML 파일을 가져올 때마다 코드 블록을 반복하고 있습니다. 대신이 폴더의 모든 HTML 파일을 반복하고 출력을 생성하기 위해 하나씩 입력 파일로 가져 가고 싶습니다. 내 현재 코드 파일 이름도 part_1.html 같이 고정되어 있지만 다를 수 있으며이 경우이 코드는 작동하지 않으며 문제가 발생합니다. 누구든지이 문제에 도움을 청할 수 있습니까? 감사합니다!Java : 디렉토리의 HTML 파일을 반복합니다.

현재 자바 코드 (두 파일의 샘플) :

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

답변

1

당신은 당신이 inXML, inXSL, outTXT을하는 방법을 작성해야

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

      } 
     } 
     }  
    } 
    } 
} 
+0

해답을 제공해 주셔서 감사합니다.이 솔루션을 사용해 다음 출력을 얻었습니다. C : \ Users \ omsairam \ workspace \ XSLT \에 있습니다. C : \ Users \ omsairam \ workspace \ XSLT \. 파일 .classpath 파일의 .project 파일 part_1.html C : \ 사용자 \ omsairam 작업 공간 \의 XSLT의 \ \ \ part_1.html C :. \ 사용자 \ omsairam 작업 공간 \의 XSLT의 \ \ \ part_1.xsl C. : \ 사용자 \ omsairam \ workspace \ XSLT \. \ part_1.txt C : \ Users \ omsairam \ workspace \ XSLT \. \ part_1.html 파일 part_2.html C : \ Users \ omsairam \ workspace \ XSLT \. \ part22.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

+0

XSLT (.xsl) 입력 파일은 모든 경우에 동일하게 유지되어야하며 별도의 폴더에 있습니다. 그에 따라 코드를 수정할 수 있습니까? 다시 한번 감사드립니다. – RahulD

+0

다른 'in'과'out' 폴더를 사용하면 동일한 이름과 다른 확장자 규칙을 사용합니다. 'html' 파일 만 처리됩니다. –

1

먼저 뭔가를 구현해야하고 인수로 renamedFile.

그런 다음 File 클래스의 list() 메서드를 사용하면 결국 FilenameFilter이됩니다. 변환하려는 파일을 반복 할 수 있습니다. 여기

FilenameFilter의 샘플입니다

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

감사

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) { 
    ... 
}