-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchaospage.go
43 lines (37 loc) · 829 Bytes
/
chaospage.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
package main
import (
"bufio"
"os"
"regexp"
)
type ChaosPage struct {
XMLPath string
WebRoot string
}
func NewChaosPage(webRoot string, xmlPath string) *ChaosPage {
return &ChaosPage{WebRoot: webRoot, XMLPath: xmlPath}
}
func (c *ChaosPage) Stylesheet() (string, error) {
r := regexp.MustCompile("<\\?xml-stylesheet")
s := regexp.MustCompile("href=\"([^\"]+\\.xsl)\"")
file, err := os.Open(c.XMLPath)
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
return "", err
}
if r.MatchString(scanner.Text()) {
stylesheetName := s.FindStringSubmatch(scanner.Text())[1]
p, err := prefixWebRoot(c.WebRoot, stylesheetName)
if err != nil {
return "", err
}
return p, nil
}
}
return "", nil
}