1- /*
1+ /**
22 * Copyright 2019 Philipp Salvisberg <[email protected] > 33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- package org .utplsql .sqldev .snippet
16+ package org .utplsql .sqldev .snippet ;
1717
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
18+ import java .io .BufferedReader ;
19+ import java .io .File ;
20+ import java .io .IOException ;
21+ import java .io .InputStream ;
22+ import java .io .InputStreamReader ;
23+ import java .io .StringReader ;
24+ import java .nio .charset .Charset ;
25+ import java .nio .file .Files ;
26+ import java .nio .file .Path ;
27+ import java .nio .file .Paths ;
28+ import java .util .stream .Collectors ;
29+ import javax .xml .parsers .DocumentBuilder ;
30+ 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 ;
36+ import org .utplsql .sqldev .model .XMLTools ;
37+ import org .w3c .dom .Document ;
38+ import org .w3c .dom .NodeList ;
39+ import org .xml .sax .InputSource ;
3140
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- }
41+ @ SuppressWarnings ("all" )
42+ 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 );
68+ }
69+ }
70+
71+ public Path merge () {
72+ try {
73+ Path _xblockexpression = null ;
74+ {
75+ 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 ();
106+ }
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 ();
116+ }
117+ }
118+ _builder_2 .append ("</snippets>" );
119+ _builder_2 .newLine ();
120+ result = _builder_2 .toString ();
121+ } else {
122+ result = this .utplsqlSnippets ;
123+ }
124+ _xblockexpression = Files .write (Paths .get (this .userSnippetsFile .getAbsolutePath ()), result .getBytes ());
125+ }
126+ return _xblockexpression ;
127+ } catch (Throwable _e ) {
128+ throw Exceptions .sneakyThrow (_e );
129+ }
130+ }
131+
132+ public String getTemplate () {
133+ return this .utplsqlSnippets ;
134+ }
135+
136+ public File getFile () {
137+ return this .userSnippetsFile ;
138+ }
139+ }
0 commit comments