|
1 | | -/** |
| 1 | +/* |
2 | 2 | * Copyright 2019 Philipp Salvisberg <[email protected]> |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
25 | 25 | import java.nio.file.Files; |
26 | 26 | import java.nio.file.Path; |
27 | 27 | import java.nio.file.Paths; |
| 28 | +import java.util.logging.Logger; |
28 | 29 | import java.util.stream.Collectors; |
| 30 | + |
| 31 | +import javax.xml.XMLConstants; |
29 | 32 | import javax.xml.parsers.DocumentBuilder; |
30 | 33 | import javax.xml.parsers.DocumentBuilderFactory; |
31 | | -import oracle.dbtools.util.Resource; |
32 | | -import org.eclipse.xtend2.lib.StringConcatenation; |
33 | | -import org.eclipse.xtext.xbase.lib.Exceptions; |
34 | | -import org.eclipse.xtext.xbase.lib.ExclusiveRange; |
35 | | -import org.eclipse.xtext.xbase.lib.Extension; |
| 34 | +import javax.xml.parsers.ParserConfigurationException; |
| 35 | + |
| 36 | +import org.utplsql.sqldev.exception.GenericRuntimeException; |
36 | 37 | import org.utplsql.sqldev.model.XMLTools; |
37 | 38 | import org.w3c.dom.Document; |
38 | 39 | import org.w3c.dom.NodeList; |
39 | 40 | import org.xml.sax.InputSource; |
| 41 | +import org.xml.sax.SAXException; |
| 42 | + |
| 43 | +import oracle.dbtools.util.Resource; |
40 | 44 |
|
41 | | -@SuppressWarnings("all") |
42 | 45 | public class SnippetMerger { |
43 | | - @Extension |
44 | | - private final XMLTools xmlTools = new XMLTools(); |
45 | | - |
46 | | - private File userSnippetsFile; |
47 | | - |
48 | | - private String utplsqlSnippets; |
49 | | - |
50 | | - public String getUtplsqlSnippetsAsString() throws IOException { |
51 | | - final InputStream stream = this.getClass().getResourceAsStream("/org/utplsql/sqldev/resources/UtplsqlSnippets.xml"); |
52 | | - Charset _defaultCharset = Charset.defaultCharset(); |
53 | | - InputStreamReader _inputStreamReader = new InputStreamReader(stream, _defaultCharset); |
54 | | - final BufferedReader reader = new BufferedReader(_inputStreamReader); |
55 | | - return reader.lines().collect(Collectors.joining(System.lineSeparator())); |
56 | | - } |
57 | | - |
58 | | - public SnippetMerger() { |
59 | | - this(new File(((Resource.RAPTOR_USER.getAbsolutePath() + File.separator) + "UserSnippets.xml"))); |
60 | | - } |
61 | | - |
62 | | - public SnippetMerger(final File file) { |
63 | | - try { |
64 | | - this.utplsqlSnippets = this.getUtplsqlSnippetsAsString(); |
65 | | - this.userSnippetsFile = file; |
66 | | - } catch (Throwable _e) { |
67 | | - throw Exceptions.sneakyThrow(_e); |
| 46 | + private static final Logger logger = Logger.getLogger(SnippetMerger.class.getName()); |
| 47 | + |
| 48 | + private final XMLTools xmlTools = new XMLTools(); |
| 49 | + private File userSnippetsFile; |
| 50 | + private String utplsqlSnippets; |
| 51 | + |
| 52 | + public String getUtplsqlSnippetsAsString() { |
| 53 | + final InputStream stream = getClass() |
| 54 | + .getResourceAsStream("/org/utplsql/sqldev/resources/UtplsqlSnippets.xml"); |
| 55 | + final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset())); |
| 56 | + return reader.lines().collect(Collectors.joining(System.lineSeparator())); |
| 57 | + } |
| 58 | + |
| 59 | + public SnippetMerger() { |
| 60 | + // works in SQL Developer only, otherwise a ExceptionInInitializerError is thrown |
| 61 | + this(new File(Resource.RAPTOR_USER.getAbsolutePath() + File.separator + "UserSnippets.xml")); |
| 62 | + } |
| 63 | + |
| 64 | + public SnippetMerger(final File file) { |
| 65 | + utplsqlSnippets = getUtplsqlSnippetsAsString(); |
| 66 | + userSnippetsFile = file; |
| 67 | + } |
| 68 | + |
| 69 | + private byte[] readFile(Path path) { |
| 70 | + try { |
| 71 | + return Files.readAllBytes(path); |
| 72 | + } catch (IOException e) { |
| 73 | + final String msg = "Cannot read file " + path.toString() + " due to " + e.getMessage() + "."; |
| 74 | + logger.severe(() -> msg); |
| 75 | + throw new GenericRuntimeException(msg, e); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private void writeFile(Path path, byte[] bytes) { |
| 80 | + try { |
| 81 | + Files.write(path, bytes); |
| 82 | + } catch (IOException e) { |
| 83 | + final String msg = "Cannot write file " + path.toString() + " due to " + e.getMessage() + "."; |
| 84 | + logger.severe(() -> msg); |
| 85 | + throw new GenericRuntimeException(msg, e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private DocumentBuilder createDocumentBuilder() { |
| 90 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 91 | + try { |
| 92 | + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); |
| 93 | + return factory.newDocumentBuilder(); |
| 94 | + } catch (ParserConfigurationException e) { |
| 95 | + final String msg = "Could not create no document builder due to " + e.getMessage() + "."; |
| 96 | + logger.severe(() -> msg); |
| 97 | + throw new GenericRuntimeException(msg, e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private Document parse(final DocumentBuilder builder, final InputSource inputSource) { |
| 102 | + try { |
| 103 | + return builder.parse(inputSource); |
| 104 | + } catch (SAXException | IOException e) { |
| 105 | + final String msg = "Could not parse XML input due to " + e.getMessage() + "."; |
| 106 | + logger.severe(() -> msg); |
| 107 | + throw new GenericRuntimeException(msg, e); |
| 108 | + } |
68 | 109 | } |
69 | | - } |
70 | | - |
71 | | - public Path merge() { |
72 | | - try { |
73 | | - Path _xblockexpression = null; |
74 | | - { |
| 110 | + |
| 111 | + public void merge() { |
75 | 112 | String result = null; |
76 | | - boolean _exists = this.userSnippetsFile.exists(); |
77 | | - if (_exists) { |
78 | | - byte[] _readAllBytes = Files.readAllBytes(Paths.get(this.userSnippetsFile.getAbsolutePath())); |
79 | | - final String userSnippets = new String(_readAllBytes); |
80 | | - final DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
81 | | - StringReader _stringReader = new StringReader(userSnippets); |
82 | | - InputSource _inputSource = new InputSource(_stringReader); |
83 | | - final Document userSnippetsDoc = docBuilder.parse(_inputSource); |
84 | | - StringConcatenation _builder = new StringConcatenation(); |
85 | | - _builder.append("/snippets/group[not(@category=\"utPLSQL Annotations\" or @category=\"utPLSQL Expectations\")]"); |
86 | | - final NodeList userSnippetsGroups = this.xmlTools.getNodeList(userSnippetsDoc, _builder.toString()); |
87 | | - StringReader _stringReader_1 = new StringReader(this.utplsqlSnippets); |
88 | | - InputSource _inputSource_1 = new InputSource(_stringReader_1); |
89 | | - final Document utplsqlSnippetsDoc = docBuilder.parse(_inputSource_1); |
90 | | - StringConcatenation _builder_1 = new StringConcatenation(); |
91 | | - _builder_1.append("/snippets/group"); |
92 | | - final NodeList utplsqlSnippetsGroups = this.xmlTools.getNodeList(utplsqlSnippetsDoc, _builder_1.toString()); |
93 | | - StringConcatenation _builder_2 = new StringConcatenation(); |
94 | | - _builder_2.append("<?xml version = \'1.0\' encoding = \'UTF-8\'?>"); |
95 | | - _builder_2.newLine(); |
96 | | - _builder_2.append("<snippets>"); |
97 | | - _builder_2.newLine(); |
98 | | - { |
99 | | - int _length = userSnippetsGroups.getLength(); |
100 | | - ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _length, true); |
101 | | - for(final Integer i : _doubleDotLessThan) { |
102 | | - _builder_2.append(" "); |
103 | | - String _nodeToString = this.xmlTools.nodeToString(userSnippetsGroups.item((i).intValue()), "code"); |
104 | | - _builder_2.append(_nodeToString, " "); |
105 | | - _builder_2.newLineIfNotEmpty(); |
| 113 | + if (userSnippetsFile.exists()) { |
| 114 | + // file exists, proper merge required |
| 115 | + final String userSnippets = new String(readFile(Paths.get(userSnippetsFile.getAbsolutePath()))); |
| 116 | + final DocumentBuilder docBuilder = createDocumentBuilder(); |
| 117 | + final Document userSnippetsDoc = parse(docBuilder, new InputSource(new StringReader(userSnippets))); |
| 118 | + final NodeList userSnippetsGroups = xmlTools.getNodeList(userSnippetsDoc, |
| 119 | + "/snippets/group[not(@category=\"utPLSQL Annotations\" or @category=\"utPLSQL Expectations\")]"); |
| 120 | + final Document utplsqlSnippetsDoc = parse(docBuilder, new InputSource(new StringReader(utplsqlSnippets))); |
| 121 | + final NodeList utplsqlSnippetsGroups = xmlTools.getNodeList(utplsqlSnippetsDoc, "/snippets/group"); |
| 122 | + StringBuilder sb = new StringBuilder(); |
| 123 | + sb.append("<?xml version = '1.0' encoding = 'UTF-8'?>\n"); |
| 124 | + sb.append("<snippets>\n"); |
| 125 | + for (int i = 0; i < userSnippetsGroups.getLength(); i++) { |
| 126 | + sb.append(" "); |
| 127 | + sb.append(xmlTools.nodeToString(userSnippetsGroups.item(i), "code")); |
| 128 | + sb.append('\n'); |
106 | 129 | } |
107 | | - } |
108 | | - { |
109 | | - int _length_1 = utplsqlSnippetsGroups.getLength(); |
110 | | - ExclusiveRange _doubleDotLessThan_1 = new ExclusiveRange(0, _length_1, true); |
111 | | - for(final Integer i_1 : _doubleDotLessThan_1) { |
112 | | - _builder_2.append(" "); |
113 | | - String _nodeToString_1 = this.xmlTools.nodeToString(utplsqlSnippetsGroups.item((i_1).intValue()), "code"); |
114 | | - _builder_2.append(_nodeToString_1, " "); |
115 | | - _builder_2.newLineIfNotEmpty(); |
| 130 | + for (int i = 0; i < utplsqlSnippetsGroups.getLength(); i ++) { |
| 131 | + sb.append(" "); |
| 132 | + sb.append(xmlTools.nodeToString(utplsqlSnippetsGroups.item(i), "code")); |
| 133 | + sb.append('\n'); |
116 | 134 | } |
117 | | - } |
118 | | - _builder_2.append("</snippets>"); |
119 | | - _builder_2.newLine(); |
120 | | - result = _builder_2.toString(); |
| 135 | + sb.append("</snippets>\n"); |
| 136 | + result = sb.toString(); |
121 | 137 | } else { |
122 | | - result = this.utplsqlSnippets; |
| 138 | + // just copy |
| 139 | + result = utplsqlSnippets; |
123 | 140 | } |
124 | | - _xblockexpression = Files.write(Paths.get(this.userSnippetsFile.getAbsolutePath()), result.getBytes()); |
125 | | - } |
126 | | - return _xblockexpression; |
127 | | - } catch (Throwable _e) { |
128 | | - throw Exceptions.sneakyThrow(_e); |
| 141 | + writeFile(Paths.get(userSnippetsFile.getAbsolutePath()), result.getBytes()); |
| 142 | + } |
| 143 | + |
| 144 | + public String getTemplate() { |
| 145 | + return utplsqlSnippets; |
| 146 | + } |
| 147 | + |
| 148 | + public File getFile() { |
| 149 | + return userSnippetsFile; |
129 | 150 | } |
130 | | - } |
131 | | - |
132 | | - public String getTemplate() { |
133 | | - return this.utplsqlSnippets; |
134 | | - } |
135 | | - |
136 | | - public File getFile() { |
137 | | - return this.userSnippetsFile; |
138 | | - } |
139 | 151 | } |
0 commit comments