Skip to content

Commit 70a076a

Browse files
class to merge UtplsqlSnippets.xml with UserSnippets.xml
1 parent 5c8717a commit 70a076a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2019 Philipp Salvisberg <[email protected]>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.utplsql.sqldev.snippet
17+
18+
import java.io.BufferedReader
19+
import java.io.File
20+
import java.io.IOException
21+
import java.io.InputStreamReader
22+
import java.io.StringReader
23+
import java.nio.charset.Charset
24+
import java.nio.file.Files
25+
import java.nio.file.Paths
26+
import java.util.stream.Collectors
27+
import javax.xml.parsers.DocumentBuilderFactory
28+
import oracle.dbtools.util.Resource
29+
import org.utplsql.sqldev.model.XMLTools
30+
import org.xml.sax.InputSource
31+
32+
class SnippetMerger {
33+
val extension XMLTools xmlTools = new XMLTools
34+
File userSnippetsFile
35+
String utplsqlSnippets
36+
37+
def getUtplsqlSnippetsAsString() throws IOException {
38+
val stream = class.getResourceAsStream("/org/utplsql/sqldev/resources/UtplsqlSnippets.xml")
39+
val reader = new BufferedReader(new InputStreamReader(stream, Charset.defaultCharset))
40+
return reader.lines.collect(Collectors.joining(System.lineSeparator))
41+
}
42+
43+
new() {
44+
// works in SQL Developer only, otherwise a ExceptionInInitializerError is thrown
45+
this (new File(Resource.RAPTOR_USER.absolutePath + File.separator + "UserSnippets.xml"))
46+
}
47+
48+
new(File file) {
49+
utplsqlSnippets = utplsqlSnippetsAsString
50+
userSnippetsFile = file
51+
}
52+
53+
def merge() {
54+
var String result
55+
if (userSnippetsFile.exists) {
56+
// file exists, proper merge required
57+
val userSnippets = new String(Files.readAllBytes(Paths.get(userSnippetsFile.absolutePath)))
58+
val docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
59+
val userSnippetsDoc = docBuilder.parse(new InputSource(new StringReader(userSnippets)))
60+
val userSnippetsGroups = userSnippetsDoc.getNodeList('''/snippets/group[not(@category="utPLSQL Annotations" or @category="utPLSQL Expectations")]''')
61+
val utplsqlSnippetsDoc = docBuilder.parse(new InputSource(new StringReader(utplsqlSnippets)))
62+
val utplsqlSnippetsGroups = utplsqlSnippetsDoc.getNodeList('''/snippets/group''')
63+
result = '''
64+
<?xml version = '1.0' encoding = 'UTF-8'?>
65+
<snippets>
66+
«FOR i : 0 ..< userSnippetsGroups.length»
67+
«userSnippetsGroups.item(i).nodeToString("code")»
68+
«ENDFOR»
69+
«FOR i : 0 ..< utplsqlSnippetsGroups.length»
70+
«utplsqlSnippetsGroups.item(i).nodeToString("code")»
71+
«ENDFOR»
72+
</snippets>
73+
'''
74+
} else {
75+
// just copy
76+
result = utplsqlSnippets
77+
78+
}
79+
Files.write(Paths.get(userSnippetsFile.absolutePath), result.bytes)
80+
}
81+
82+
def getTemplate() {
83+
return utplsqlSnippets
84+
}
85+
86+
def getFile() {
87+
return userSnippetsFile
88+
}
89+
90+
}

0 commit comments

Comments
 (0)