-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjnlp.go
157 lines (142 loc) · 3.72 KB
/
jnlp.go
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
package main
import (
"io"
"net/url"
"github.com/antchfx/xmlquery"
)
// JnlpFile represents a JNLP descriptor
type JnlpFile struct {
Codebase string
Base *url.URL
Root *xmlquery.Node
}
// DownloadType constants resprenting resource download attribute values
type DownloadType int8
const (
// Eager download type
Eager DownloadType = 0
// Progress download type
Progress DownloadType = 1
// Lazy download type
Lazy DownloadType = 2
)
// JarResource represents a jar resource in a JNLP descriptor
type JarResource struct {
Href string
Main bool
Download DownloadType
}
// ApplicationDesc represents an application-desc in a JNLP descriptor
type ApplicationDesc struct {
MainClass string
Arguments []string
}
// Property represents a property resource in a JNLP descriptor
type Property struct {
Name string
Value string
}
// J2SE represents a j2se resource in a JNLP descriptor
type J2SE struct {
Href string
Version string
InitialHeapSize string
MaxHeapSize string
}
// ParseJnlp parses the JNLP descriptor from the given input
func ParseJnlp(reader io.Reader) (*JnlpFile, error) {
doc, err := xmlquery.Parse(reader)
if err != nil {
return nil, err
}
node, err := xmlquery.Query(doc, "jnlp")
if err != nil {
return nil, err
}
codebase := node.SelectAttr("codebase")
base, err := url.Parse(codebase)
if err != nil {
return nil, err
}
return &JnlpFile{
Codebase: codebase,
Base: base,
Root: node,
}, nil
}
// GetJarResources specifies all the jar resources contained in the JNLP descriptor
func (x *JnlpFile) GetJarResources() ([]*JarResource, error) {
// Get jars
jars := xmlquery.Find(x.Root, "resources/jar")
jarResources := make([]*JarResource, len(jars))
for i := 0; i < len(jars); i++ {
jar := jars[i]
href := jar.SelectAttr("href")
main := jar.SelectAttr("main")
download := jar.SelectAttr("download")
bMain := false
if main == "true" {
bMain = true
}
iDownload := Eager
switch download {
case "progress":
iDownload = Progress
break
case "lazy":
iDownload = Lazy
break
}
jarResources[i] = &JarResource{
Href: href,
Main: bMain,
Download: iDownload,
}
}
return jarResources, nil
}
// GetJarURL specifies the full URL for the given jar resource that may be used for downloading
func (x *JnlpFile) GetJarURL(jar *JarResource) (*url.URL, error) {
u, err := x.Base.Parse(jar.Href)
return u, err
}
// GetProperties specifies all the property resources contained in the JNLP descriptor
func (x *JnlpFile) GetProperties() []*Property {
props := xmlquery.Find(x.Root, "resources/property")
arr := make([]*Property, len(props))
for i := 0; i < len(props); i++ {
arr[i] = &Property{
Name: props[i].SelectAttr("name"),
Value: props[i].SelectAttr("value"),
}
}
return arr
}
// GetJ2SE specifies the j2se resource contained in the JNLP descriptor
func (x *JnlpFile) GetJ2SE() *J2SE {
elem := xmlquery.FindOne(x.Root, "resources/j2se")
href := elem.SelectAttr("href")
version := elem.SelectAttr("version")
initialHeapSize := elem.SelectAttr("initial-heap-size")
maxHeapSize := elem.SelectAttr("max-heap-size")
return &J2SE{
Href: href,
Version: version,
InitialHeapSize: initialHeapSize,
MaxHeapSize: maxHeapSize,
}
}
// GetApplicationDesc specifies the application-desc contained in the JNLP descriptor
func (x *JnlpFile) GetApplicationDesc() *ApplicationDesc {
desc := xmlquery.FindOne(x.Root, "application-desc")
mainClass := desc.SelectAttr("main-class")
args := xmlquery.Find(desc, "argument")
argvals := make([]string, len(args))
for i := 0; i < len(args); i++ {
argvals[i] = args[i].InnerText()
}
return &ApplicationDesc{
MainClass: mainClass,
Arguments: argvals,
}
}