Package: AppleScriptCaptureOneDao
AppleScriptCaptureOneDao
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
AppleScriptCaptureOneDao() |
|
|
|
|
|
||||||||||||||||||||
createEngine() |
|
|
|
|
|
||||||||||||||||||||
execute(String) |
|
|
|
|
|
||||||||||||||||||||
getDocument() |
|
|
|
|
|
||||||||||||||||||||
getDocumentAsXml() |
|
|
|
|
|
||||||||||||||||||||
getTestAsXml() |
|
|
|
|
|
||||||||||||||||||||
parse(String) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.captureone.impl.applescript;
28:
29: import javax.annotation.Nonnull;
30: import java.io.IOException;
31: import java.io.InputStreamReader;
32: import java.io.PrintWriter;
33: import java.io.Reader;
34: import java.io.StringReader;
35: import jakarta.xml.bind.JAXBContext;
36: import jakarta.xml.bind.JAXBException;
37: import javax.script.ScriptContext;
38: import javax.script.ScriptEngine;
39: import javax.script.ScriptEngineManager;
40: import javax.script.ScriptException;
41: import it.tidalwave.bluemarine2.captureone.CaptureOneDao;
42: import it.tidalwave.bluemarine2.captureone.impl.C1Collection;
43: import it.tidalwave.bluemarine2.captureone.impl.C1Document;
44: import it.tidalwave.bluemarine2.captureone.impl.C1Image;
45: import static java.nio.charset.StandardCharsets.UTF_8;
46:
47: /***********************************************************************************************************************
48: *
49: * An implementation of {@link CaptureOneDao} based on Applescript.
50: *
51: * @author Fabrizio Giudici
52: *
53: **********************************************************************************************************************/
54: public class AppleScriptCaptureOneDao implements CaptureOneDao
55: {
56: /*******************************************************************************************************************
57: *
58: * {@inheritDoc}
59: *
60: ******************************************************************************************************************/
61: @Override @Nonnull
62: public C1Document getDocument()
63: throws IOException
64: {
65: try
66: {
67: return parse(getDocumentAsXml());
68: }
69: catch (IOException | ScriptException | JAXBException e)
70: {
71: throw new IOException(e);
72: }
73: }
74:
75: /*******************************************************************************************************************
76: *
77: *
78: *
79: ******************************************************************************************************************/
80: @Nonnull
81: /* package */ C1Document parse (@Nonnull final String xml)
82: throws JAXBException
83: {
84: final JAXBContext context = JAXBContext.newInstance(C1Document.class, C1Collection.class, C1Image.class);
85: return (C1Document)context.createUnmarshaller().unmarshal(new StringReader(xml));
86: }
87:
88: /*******************************************************************************************************************
89: *
90: *
91: *
92: ******************************************************************************************************************/
93: @Nonnull
94: /* package */ String getDocumentAsXml()
95: throws IOException, ScriptException
96: {
97: return execute("Capture One.scpt");
98: }
99:
100: /*******************************************************************************************************************
101: *
102: *
103: *
104: ******************************************************************************************************************/
105: @Nonnull
106: /* package */ String getTestAsXml()
107: throws IOException, ScriptException
108: {
109: return execute("Test.scpt");
110: }
111:
112: /*******************************************************************************************************************
113: *
114: *
115: *
116: ******************************************************************************************************************/
117: @Nonnull
118: private static String execute (final String scriptName)
119: throws IOException, ScriptException
120: {
121: final ScriptEngine se = createEngine();
122:
123: try (final Reader sr = new InputStreamReader(AppleScriptCaptureOneDao.class.getResourceAsStream(scriptName), UTF_8))
124: {
125: return se.eval(sr, se.getContext()).toString();
126: }
127: }
128:
129: /*******************************************************************************************************************
130: *
131: *
132: *
133: ******************************************************************************************************************/
134: @Nonnull
135: private static ScriptEngine createEngine()
136: {
137: final ScriptEngineManager sem = new ScriptEngineManager();
138: final ScriptEngine se = sem.getEngineByName("AppleScript");
139: final ScriptContext context = se.getContext();
140: context.setErrorWriter(new PrintWriter(System.err, true));
141: context.setWriter(new PrintWriter(System.out));
142: return se;
143: }
144: }