parser

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2018 License: MIT Imports: 13 Imported by: 1

Documentation

Overview

Package parser provides implementations of xml parsers.

It's main purpose is to provide xml parser which is suitable for generating xtree structure used in xml comparison with xdiff algorithm.

There are two parser implementations available:

  • The standard implementation using encoding/xml library,
  • and the custom in-place parser which is much faster but less robust.

Use standard parser if you need robust and well tested parser and you don't care about it's performance. Use custom in-place parser if you need fast performance but can sacrifice robustness. In each case test it first on your own xml data.

There are three functionalities that require xdiff xtree parser:

  • Assign signatures and hashes to the nodes,
  • Parse directories as nodes,
  • Parse attributes as nodes,
  • Optimizing execution speed and resource usage, for more info see https://github.com/golang/go/issues/21823

Example API usage:

  p := parser.New()
  // to parse from reader
	 xtree, err := p.ParseReader(openedFileReader)
  // to parse from bytes
	 xtree, err := p.ParseBytes(filebytes)
  // to parse from filepath
  xtree, err := p.ParseFile("filename.xml")
  // to parse from dirpath
	 xtree, err := p.ParseDir("dirname")

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedEnd = errors.New("parser: unexpected end of data")

ErrUnexpectedEnd represents attempted read beyond available data.

Functions

This section is empty.

Types

type Standard

type Standard struct {
	// Handler for non-xml files encountered during directory traversal.
	NonXMLHandler func(f *os.File, fi os.FileInfo) (*xtree.Node, error)
}

Standard is a standard parser using encoding/xml sax parser as engine.

func NewStandard

func NewStandard() *Standard

NewStandard instantiates new standard parser.

func (*Standard) ParseBytes

func (p *Standard) ParseBytes(b []byte) (*xtree.Node, error)

ParseBytes returns reference to the document node parsed from provided bytes.

func (*Standard) ParseDir

func (p *Standard) ParseDir(dirpath string) (*xtree.Node, error)

ParseDir returns reference to the directory node got by parsing provided dirpath. Each directory is parent to the child xml documents and documents have xtree structure got by parsing xml.

RootDir |- Child1.xml | `- rootElement | |- childElement | `- childElement

 `- Child2.xml
	 `- rootElement

Use NonXMLHandler flag on the parser to configure behavior for handling non-xml files found in the directories.

func (*Standard) ParseFile

func (p *Standard) ParseFile(filepath string) (*xtree.Node, error)

ParseFile returns reference to the document node got by parsing provided filepath.

func (*Standard) ParseReader

func (p *Standard) ParseReader(r io.Reader) (*xtree.Node, error)

ParseReader returns reference to the document node got by parsing bytes from the provided reader.

type XDiff

type XDiff struct {
	// Whether or not to validate closing tags.
	ValidateClosingTag bool
	// Handler for non-xml files encountered during directory traversal.
	NonXMLHandler func(f *os.File, fi os.FileInfo) (*xtree.Node, error)
	// Set document node name to filename when parsing xml by filename.
	SetDocumentFilename bool
	// contains filtered or unexported fields
}

XDiff is a custom XML parser able to parse xml files to generate structures needed for doing xtree comparison in XDiff algorithm.

Besides standard xml parsing, parsed xtree is also able to include nodes formed by the directory structure in which xml files reside. This is useful for comparing xml data which is spread across different directories and files.

Parser is doing only basic transformation on the xml document to preserve documents as is for better comparison.

func New

func New() *XDiff

New instantiates new parser.

func (*XDiff) ParseBytes

func (p *XDiff) ParseBytes(b []byte) (*xtree.Node, error)

ParseBytes returns reference to the document node parsed from provided bytes. For performance reasons returned structure will reuse byte array from the provided slice. Do not manipulate with it until you are done using the parsed structure. Pass the copy of the slice if you want to maintain ownership of the bytes.

func (*XDiff) ParseDir

func (p *XDiff) ParseDir(path string) (*xtree.Node, error)

ParseDir returns reference to the directory node got by parsing provided dirpath. Each directory is parent to the child xml documents and documents have xtree structure got by parsing xml.

RootDir |- Child1.xml | `- rootElement | |- childElement | `- childElement

 `- Child2.xml
	 `- rootElement

Use NonXMLHandler flag on the parser to configure behavior for handling non-xml files found in the directories.

func (*XDiff) ParseFile

func (p *XDiff) ParseFile(filepath string) (*xtree.Node, error)

ParseFile returns reference to the document node got by parsing provided filepath.

func (*XDiff) ParseReader

func (p *XDiff) ParseReader(r io.Reader) (*xtree.Node, error)

ParseReader returns reference to the document node got by parsing bytes from the provided reader.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL