Skip to main content
summaryrefslogtreecommitdiffstats
blob: 57dcb2f419d3cd6a3f60184d8dce8d10c7fde875 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
[comment encoding = UTF-8 /]
[comment
/*******************************************************************************
 * Copyright (c) 2014 Jad El-khoury.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 *
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *
 *     Jad El-khoury        - initial implementation of code generator (https://bugs.eclipse.org/bugs/show_bug.cgi?id=422448)
 *    
 *******************************************************************************/
/]

[module services('http://org.eclipse.lyo/oslc4j/adaptorInterface')/]

[query private javaKeywords(traceabilityContext : OclAny) : Set(String) =
Set{'abstract', 'continue', 'for', 'new', 'switch', 'assert', 'default', 'goto', 'package', 'synchronized', 'boolean', 'do',
'if', 'private', 'this', 'break', 'double', 'implements', 'protected', 'throw', 'byte', 'else', 'import', 'public', 'throws',
'case', 'enum', 'instanceof', 'return', 'transient', 'catch', 'extends', 'int', 'short', 'try', 'char', 'final', 'interface',
'static', 'void', 'class', 'finally', 'long', 'strictfp', 'volatile', 'const', 'float', 'native', 'super', 'while'}
/]

[query private javaLangWords(traceabilityContext : OclAny) : Set(String) =
Set{'appendable', 'autocloseable', 'charsequence', 'cloneable', 'comparable', 'iterable', 'readable', 'runnable', 'thread',
'boolean', 'byte', 'character', 'class', 'classloader', 'classvalue', 'compiler', 'double', 'enum', 'float',
'inheritablethreadlocal', 'integer', 'long', 'math', 'number', 'object', 'package', 'process', 'processbuilder',
'runtime', 'runtimepermission', 'securitymanager', 'short', 'stacktraceelement', 'strictmath', 'string', 'stringbuffer',
'stringbuilder', 'system', 'thread', 'threadgroup', 'threadlocal', 'throwable', 'void'}
/]

[query private isJavaSpecialWord(aString : String) : Boolean =
((javaKeywords(aString)->union(javaLangWords(aString)))->includes(aString.toLower()))
/]

[query public javaClassBaseNamespace(anAdaptorInterface : AdaptorInterface) : String =
anAdaptorInterface.javaClassBaseNamespace
/]

[query public javaFilesBasePath(anAdaptorInterface : AdaptorInterface) : String =
anAdaptorInterface.javaFilesBasePath
/]

[query public jspFilesBasePath(anAdaptorInterface : AdaptorInterface) : String =
anAdaptorInterface.jspFilesBasePath
/]

[query public javascriptFilesBasePath(anAdaptorInterface : AdaptorInterface) : String =
anAdaptorInterface.javascriptFilesBasePath
/]


[query public jspFilesWebappPath(anAdaptorInterface : AdaptorInterface) : String =
'/'.concat(javaClassBaseNamespace(anAdaptorInterface)).substituteAll('.', '/')
/]

[query public methodSignature(parameters: Sequence(String), forJAXRSMethod : Boolean) : String = 
concatenate(
    parameters->collect(aParameter: String |
                        (if (forJAXRSMethod) then
                            '@PathParam("'.concat(aParameter).concat('") final String ').concat(aParameter)
                        else
                            'final String '.concat(aParameter)
                        endif)
    )->sep(', ').oclAsType(String))
/]

[query public methodParameterList(parameters: Sequence(String)) : String = 
concatenate(parameters->sep(', ').oclAsType(String))
/]

[query public JAXRSPathAnnotation(pathAnnotation : String) : String =
    (if (pathAnnotation.size() >= 1) then
        '@Path("'.concat(pathAnnotation).concat('")')
    else
        ''
    endif)
/]

[query public JAXRSPathParameters(uriSegment : String) : Sequence(String) =
uriSegment.tokenize('}')
    ->select(token : String | token.index('{') <> -1)
    ->collect(token : String | token.substring(token.index('{')+1))
/]

[query private stripURISegmentEnd(URISegment : String) : String =
(if (URISegment.size() = 0) then
     URISegment
else
    (if (URISegment.size() = 1) then
         (if (URISegment.endsWith('/')) then '' else URISegment endif)
    else
        URISegment.substring(1, URISegment.size()-(if (URISegment.endsWith('/')) then 1 else 0 endif))
    endif)
endif)
/]

[query private stripURISegmentStart(URISegment : String) : String =
(if (URISegment.size() = 0) then
     URISegment
else
    (if (URISegment.size() = 1) then
         (if (URISegment.startsWith('/')) then '' else URISegment endif)
    else
        URISegment.substring((if (URISegment.startsWith('/')) then 2 else 1 endif), URISegment.size())
    endif)
endif)
/]

[query public JAXRSConcatURISegments(leftURISegment : String, rightURISegment : String) : String =
    concatenatePaths(leftURISegment, rightURISegment)
/]

[query public concatenatePaths (leftSegment : String, rightSegment : String) : String =
(if (stripURISegmentEnd(leftSegment).size() = 0) then
    stripURISegmentStart(rightSegment)
else
    (if (stripURISegmentStart(rightSegment).size() = 0) then
        stripURISegmentEnd(leftSegment)
    else
        stripURISegmentEnd(leftSegment).concat('/').concat(stripURISegmentStart(rightSegment))
    endif)
endif)
/]

[query public commaSeparate(commaSeparatedString : String, prependIfNotEmpty : Boolean, appendIfNotEmpty : Boolean) : String =
(if prependIfNotEmpty._and(commaSeparatedString.size() > 0) then ', ' else ''endif)
.concat(commaSeparatedString)
.concat(
(if appendIfNotEmpty._and(commaSeparatedString.size() > 0) then ' ,' else ''endif)
)
/]

[query public javaSimpleStringFromUri(uriString: String, toUpperFirst : Boolean) : String =
javaString(uriString.tokenize('/\\#')->last(), '', toUpperFirst)
/]

[query public javaConstantString (aString : String) : String =
concatenate(
    aString.replaceAll('\\W+', '_').tokenize('_')->sep('_').oclAsType(String)
).toUpperCase()
/]

[query private javaString (aString : String) : String =
concatenate(
    aString.replaceAll('\\W+', ' ').tokenize(' ')->collect(s : String  | s.toUpperFirst())
)
/]

[query public javaString (aString : String, prefixIfNecessary : String, toUpperFirst : Boolean) : String =
let base : String = (if (isJavaSpecialWord(aString)) then prefixIfNecessary.concat(javaString(aString).toUpperFirst()) else javaString(aString) endif)
in 
if (toUpperFirst) then base.toUpperFirst() else base.toLowerFirst() endif
/]

[query public concatenate (aStringSequence : Sequence(String)) : String =
(if (aStringSequence->size() = 0) then
    ''
else
    (if (aStringSequence->size() = 1) then
        aStringSequence->at(1)
    else
        (if (aStringSequence->size() = 2) then
            aStringSequence->at(1).concat(aStringSequence->at(2))
        else
            aStringSequence->at(1).concat(aStringSequence->at(2)).concat(concatenate(aStringSequence->subSequence(3, aStringSequence->size())))
        endif)
    endif)
endif)
/]

[query public concatenate (aStringSequence : Sequence(String), separatorString : String) : String =
    concatenate(aStringSequence->sep(separatorString).oclAsType(String))
/]

[query public isNullOrEmpty (aString : String) : Boolean =
(aString.oclIsUndefined())._or(aString.trim().equalsIgnoreCase(''))
/]

Back to the top