-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework loading of xsd schema and its imports
It seems there are some circular dependencies in OVAL XSDs and hence we need extra layer that avoids re-opening already loaded schemas
- Loading branch information
Showing
3 changed files
with
54 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package xsd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type Workspace struct { | ||
PrimarySchema *Schema | ||
Cache map[string]*Schema | ||
} | ||
|
||
func NewWorkspace(xsdPath string) (*Workspace, error) { | ||
ws := Workspace{Cache: map[string]*Schema{}} | ||
var err error | ||
ws.PrimarySchema, err = ws.loadXsd(xsdPath) | ||
return &ws, err | ||
} | ||
|
||
func (ws *Workspace) loadXsd(xsdPath string) (*Schema, error) { | ||
cached, found := ws.Cache[xsdPath] | ||
if found { | ||
return cached, nil | ||
} | ||
fmt.Println("\tParsing:", xsdPath) | ||
|
||
f, err := os.Open(xsdPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
schema, err := parseSchema(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ws.Cache[xsdPath] = schema | ||
|
||
dir := filepath.Dir(xsdPath) | ||
for idx, _ := range schema.Imports { | ||
if err := schema.Imports[idx].load(ws, dir); err != nil { | ||
return nil, err | ||
} | ||
} | ||
schema.compile() | ||
return schema, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters