1   package nl.dedicon.pipeline.braille.calabash.impl;
2   
3   import com.xmlcalabash.core.XProcException;
4   import com.xmlcalabash.core.XProcRuntime;
5   import com.xmlcalabash.core.XProcStep;
6   import com.xmlcalabash.library.DefaultStep;
7   import com.xmlcalabash.runtime.XAtomicStep;
8   import java.io.File;
9   import java.io.IOException;
10  import java.net.URI;
11  import java.net.URISyntaxException;
12  import java.nio.file.Files;
13  import java.nio.file.Path;
14  import java.nio.file.StandardOpenOption;
15  import net.sf.saxon.s9api.QName;
16  import net.sf.saxon.s9api.SaxonApiException;
17  import org.apache.commons.lang3.StringUtils;
18  import org.daisy.common.xproc.calabash.XProcStepProvider;
19  import org.osgi.service.component.annotations.Component;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * XProc step for inserting a newline in BRL files
25   * 
26   * @author Paul Rambags
27   */
28  public class BrlMustStartWithNewlineStep extends DefaultStep {
29  
30      private static final Logger logger = LoggerFactory.getLogger(BrlMustStartWithNewlineStep.class);
31  
32      private static final QName _brl_uri = new QName("brl-uri");
33  
34      private BrlMustStartWithNewlineStep(XProcRuntime runtime, XAtomicStep step) {
35          super(runtime, step);
36      }
37  
38      @Override
39      public void reset() {
40      }
41  
42      @Override
43      public void run() throws SaxonApiException {
44          super.run();
45  
46          try {
47              String brlUri = getOption(_brl_uri, "");
48              if (StringUtils.isNotBlank(brlUri)) {
49                  
50                  try {
51  
52                      Path path = new File(new URI(brlUri)).toPath();
53                      // The path refers to a CP850 encoded file
54                      // A CR + LF (hex 0D 0A) is inserted if the file does not already start with it
55                      byte[] bytes = Files.readAllBytes(path);
56                      if (bytes.length > 1 && bytes[0] != 0xD && bytes[1] != 0xA) {
57                          byte[] newLine = new byte[]{ 0xD, 0xA };
58                          Files.write(path, newLine);
59                          Files.write(path, bytes, StandardOpenOption.APPEND);
60                      }
61  
62                  } catch (URISyntaxException|IOException e) {
63                      logger.error(String.format("Failed to insert a newline in file %s due to %s", brlUri, e.getMessage()));
64                      throw e;
65                  }
66              }
67          } catch (Exception e) {
68              logger.error("dedicon:brl-must-start-with-newline failed", e);
69              throw new XProcException(step.getNode(), e);
70          }
71      }
72  
73      @Component(
74              name = "dedicon:brl-must-start-with-newline",
75              service = {XProcStepProvider.class},
76              property = {"type:String={http://www.dedicon.nl}brl-must-start-with-newline"}
77      )
78      public static class Provider implements XProcStepProvider {
79  
80          @Override
81          public XProcStep newStep(XProcRuntime runtime, XAtomicStep step) {
82              return new BrlMustStartWithNewlineStep(runtime, step);
83          }
84      }
85  }