Beispiel #1
0
func TestParseXmlOneNodeWithLtError(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<he<llo>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello")
	err := saxReader.Read(reader, &tm)
	assert.NotNil(t, err)
}
Beispiel #2
0
func TestParseXmlOneNodeEmptySearch(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "")
}
Beispiel #3
0
func TestParseXmlTestS2(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello><text xml:space=\"preserve\">this</text><hello2>Test2</hello2><hello3>Test3</hello3></hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("?xml:space")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<text xml:space=\"preserve\">this</text>")
}
Beispiel #4
0
func TestParseXmlTest(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello id=\"123\" ref=\"42\"><hello2 idx=\"1234\" refx=\"421\">test</hello2></hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello?id=123&ref=42/hello2?idx=1234&refx=421")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello2 idx=\"1234\" refx=\"421\">test</hello2>")
}
Beispiel #5
0
func TestParseXmlOneNodeTwoAttributesNoMatch(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello id=\"123\" ref=\"42\">test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello?id=123&ref=421")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.NotEqual(t, res, "<hello id=\"123\" ref=\"42\">test</hello>")
}
Beispiel #6
0
func TestParseXmlOneNodeOneAttributeSingle(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello id='123'>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("hello?id=123")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello id='123'>test</hello>")
}
Beispiel #7
0
func TestParseXmlNodesWithLtEscapeTag(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><helloB>&lt;helloC>&lt;/helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("helloA/helloB")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<helloB>&lt;helloC>&lt;/helloC></helloB>")
}
Beispiel #8
0
func TestParseXmlNodeConstrainedBuffer(t *testing.T) {
	res := ""
	emitter := func(ed *contentBuffer.EmitterData) bool {
		res = ed.Content
		return false
	}
	reader := bytes.NewReader([]byte("<hello>test</hello>"))
	saxReader := newTestSaxReader(emitter)
	saxReader.ReaderBufferSize = 1
	tm := tagMatcher.NewTagMatcher("hello")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, res, "<hello>test</hello>")
}
Beispiel #9
0
func TestParseXmlNodesWithCdataAndComment(t *testing.T) {
	var actuals []string = make([]string, 10)
	var actualsPos int = 0
	emitter := func(ed *contentBuffer.EmitterData) bool {
		actuals[actualsPos] = ed.Content
		actualsPos++
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><!-- test<>--<><--><helloB><helloC><![CDATA[Hello<! World!]]></helloC><helloC>C2</helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	tm := tagMatcher.NewTagMatcher("helloA/helloB/helloC")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, actuals[0], "<helloC><![CDATA[Hello<! World!]]></helloC>")
	assert.Equal(t, actuals[1], "<helloC>C2</helloC>")
}
Beispiel #10
0
func TestParseXmlNodesConstrainedBuffer(t *testing.T) {
	var actuals []string = make([]string, 10)
	var actualsPos int = 0
	emitter := func(ed *contentBuffer.EmitterData) bool {
		actuals[actualsPos] = ed.Content
		actualsPos++
		return false
	}
	reader := bytes.NewReader([]byte("<helloA><helloB><helloC>C1</helloC><helloC>C2</helloC></helloB></helloA>"))
	saxReader := newTestSaxReader(emitter)
	saxReader.ReaderBufferSize = 1
	tm := tagMatcher.NewTagMatcher("helloA/helloB/helloC")
	err := saxReader.Read(reader, &tm)
	assert.Nil(t, err)
	assert.Equal(t, actuals[0], "<helloC>C1</helloC>")
	assert.Equal(t, actuals[1], "<helloC>C2</helloC>")
}
Beispiel #11
0
func SaxXmlInput(reader io.Reader) {
	var err error
	var sr saxReader.SaxReader
	sr = saxReader.NewSaxReaderNoEmitter()
	tm := tagMatcher.NewTagMatcher(*query)
	if *containMatch {
		tm.EqualityFn = tagMatcher.EqFnContains
	} else {
		tm.EqualityFn = tagMatcher.EqFnEqulas
	}
	tm.CaseSensitive = !*caseSesitive
	tm.WithoutNamespace = *omitNamespace
	sr.IsInnerXml = *isInnerXml
	sr.ContentBufferSize = *contentBuf * ONE_MB
	sr.ElementBufferSize = *tagBuffer * ONE_KB
	if *wrapResult {
		fmt.Println("<saxer-result>")
	}
	if *count {
		var counter uint64 = 0
		emitterCounter := func(ed *contentBuffer.EmitterData) bool {
			counter++
			return false
		}
		sr.EmitterFn = emitterCounter
		err = sr.Read(reader, &tm)
		fmt.Println(counter)
	} else if *meta {
		counter := 0
		elemChan := make(chan contentBuffer.EmitterData, 100)
		var wg sync.WaitGroup
		go emitterMetaPrinter(elemChan, &wg)
		emitter := func(ed *contentBuffer.EmitterData) bool {
			wg.Add(1)
			elemChan <- contentBuffer.EmitterData{Content: ed.Content, LineStart: ed.LineStart, LineEnd: ed.LineEnd, NodePath: ed.NodePath}
			if *firstN > 0 {
				counter++
				if counter >= *firstN {
					return true
				} else {
					return false
				}
			}
			return false
		}
		sr.EmitterFn = emitter
		err = sr.Read(reader, &tm)
		wg.Wait()
	} else {
		counter := 0
		elemChan := make(chan string, 100)
		var wg sync.WaitGroup
		go emitterPrinter(elemChan, &wg, *singleLine, *unescape)
		emitter := func(ed *contentBuffer.EmitterData) bool {
			wg.Add(1)
			elemChan <- ed.Content
			if *firstN > 0 {
				counter++
				if counter >= *firstN {
					return true
				} else {
					return false
				}
			}
			return false
		}
		sr.EmitterFn = emitter
		err = sr.Read(reader, &tm)
		wg.Wait()
	}
	if *wrapResult {
		fmt.Println("</saxer-result>")
	}
	if err != nil {
		panic(err)
	}
}