Giter Site home page Giter Site logo

xml-region-analyzer's Introduction

XML Region Analyzer

Build Status Coverage Status License

A light and simple lexical analyzer for XML.

This project was created because I needed a simple way to add syntax highlighting in a stand-alone SWT application. Since there was no simple solution, I created one. Given a string representing a XML document, it finds regions and their positions.

Here is an example.

List<XmlRegion> regions = new XmlRegionAnalyzer().analyzeXml( yourXmlAsAString );
for( XmlRegion xr : regions ) {
   switch( xr.getXmlRegionType ()) {
   	case XmlRegionType.MARKUP: break;
   	case XmlRegionType.ATTRIBUTE: break;
   	case XmlRegionType.ATTRIBUTE_VALUE: break;
   	case XmlRegionType.MARKUP_VALUE: break;
   	case XmlRegionType.COMMENT: break;
   	case XmlRegionType.INSTRUCTION: break;
   	case XmlRegionType.CDATA: break;
   	case XmlRegionType.WHITESPACE: break;
   	default: break;
   }
   
   int regionLength = xr.getEnd() - xr.getStart();
   System.out.println( "Region Length:" + regionLength );
}

Using it

You can add this project in your Maven dependencies.

<dependency>
	<groupId>com.github.vincent-zurczak</groupId>
	<artifactId>xml-region-analyzer</artifactId>
	<version>1.0.0</version>
</dependency>

This project has no dependency towards other libraries.

Development

# Compile and package
mvn clean package

# Run the tests
mvn clean test

# Get code coverage (then check target/site/cobertura/)
mvn clean cobertura:cobertura

License

The source code is licensed under the terms of the Apache license v2.

xml-region-analyzer's People

Contributors

pierre-do avatar stever113322 avatar vincent-zurczak avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xml-region-analyzer's Issues

Infinite Loop in analyzeInstruction

Infinite Loop in analyzeInstruction if XML has ""

this works for me:

boolean analyzeInstruction( String xml, List positions ) {

	boolean result = false;
	int newPos = this.offset;
	if( newPos < xml.length()
			&& xml.charAt( newPos ) == '<'
			&& ++ newPos < xml.length()
			&& xml.charAt( newPos ) == '?' ) {

		while( newPos > -1 && ++ newPos < xml.length() 
				&& xml.charAt( newPos ) != '>' )
			newPos = xml.indexOf( '?', newPos );

		if( newPos > -1 && xml.charAt( newPos ) == '>' ) {
			positions.add( new XmlRegion( XmlRegionType.INSTRUCTION, this.offset, newPos + 1 ));
			this.offset = newPos + 1;
			result = true;
		}
	}

	return result;
}

And thanks for sharing!!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.