View Javadoc

1   /*
2    * Copyright (C) 2013 Room Work eXperience
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package net.rwx.maven.asciidoc.services.impl;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import net.rwx.maven.asciidoc.backends.Backend;
23  import net.rwx.maven.asciidoc.configuration.Document;
24  import net.rwx.maven.asciidoc.services.AsciidocService;
25  import net.rwx.maven.asciidoc.utils.FileUtils;
26  import org.python.core.PyString;
27  import org.python.core.PySystemState;
28  import org.python.util.PythonInterpreter;
29  
30  public class AsciidocServiceImpl extends RootServiceImpl implements AsciidocService {
31  
32      private String asciidocProgramPath;
33      
34      public AsciidocServiceImpl() throws IOException {
35          ClassLoader loader = this.getClass().getClassLoader();
36          InputStream is = loader.getResourceAsStream( ARCHIVE_NAME );
37          String temporaryDirectory = FileUtils.getTemporaryDirectory();
38          FileUtils.uncompress( is, temporaryDirectory );
39          setAsciidocProgramPath( temporaryDirectory );
40      }
41      
42      /**
43       * Launch Asciidoc script.
44       * 
45       * @param input Path to Asciidoc document to process
46       * @param backend Determine output format for Asciidoc script
47       * @param output Path to output file (file format should match backend)
48       */
49      @Override
50      public void execute( String inputPath, Document document, Backend backend ) throws Exception {
51          setOuputPath(inputPath, backend);
52          PySystemState state = getPySystemState( inputPath, backend.getName(), getOuputPath() );
53  
54          PythonInterpreter interp = new PythonInterpreter( null, state );
55          interp.set( "__file__", asciidocProgramPath );
56          interp.execfile( asciidocProgramPath );
57      }
58      
59      /**
60       * Initialize parameters and working dir for Jython interpreter.
61       * 
62       * @param input Path to Asciidoc document to process
63       * @param backend Determine output format for Asciidoc script
64       * @param output Path to output file (file format should match backend)
65       * @return a system state for Jython interpreter
66       */
67      private PySystemState getPySystemState( String input, String backend, String output ) {
68          PySystemState state = new PySystemState();
69          
70          state.argv.clear();
71          state.argv.append( new PyString( asciidocProgramPath ) );
72          state.argv.append( new PyString( "-b" ) );
73          state.argv.append( new PyString( backend ) );
74          state.argv.append( new PyString( "-a" ) );
75          state.argv.append( new PyString( "icons" ) );
76          state.argv.append( new PyString( "--out-file=" + output ) );
77          state.argv.append( new PyString( input ) );
78          
79          File fInput = new File( input );
80          state.setCurrentWorkingDir( fInput.getParent() );
81          
82          return state;
83      }
84      
85      /**
86       * Build path to Asciidoc script based on temporary directory.
87       */
88      private void setAsciidocProgramPath( String temporaryDirectory ) {
89          StringBuilder builder = new StringBuilder();
90          builder.append( temporaryDirectory );
91          builder.append( File.separator );
92          builder.append( "asciidoc-" );
93          builder.append( VERSION );
94          builder.append( File.separator );
95          builder.append( "asciidoc.py" );
96          
97          asciidocProgramPath = builder.toString();
98      }
99  
100     @Override
101     protected void setOuputPath(String inputPath, Backend backend) {
102         setOutputPath(inputPath, backend.getAsciidocExtension());
103     }
104 }