view src/goodjava/html/Css.java @ 1721:5c69d2e8bd75

no regex in String.find and String.split
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 26 Jul 2022 12:44:52 -0600
parents 31a82b0d0a87
children
line wrap: on
line source

package goodjava.html;

import java.util.Map;
import java.util.LinkedHashMap;
import goodjava.parser.Parser;


public final class Css {

	public static Map<String,String> style(String text) {
		return new Css(text).parseStyle();
	}

	private final Parser parser;

	private Css(String text) {
		this.parser = new Parser(text);
	}

	private Map<String,String> parseStyle() {
		Map<String,String> map = new LinkedHashMap<String,String>();
		while( matchSpace() );
		while( !parser.endOfInput() ) {
			int start = parser.currentIndex();
			if( !matchPropertyChar() )
				return null;
			while( matchPropertyChar() );
			String prop = parser.textFrom(start).toLowerCase();

			while( matchSpace() );
			if( !parser.match(':') )
				return null;

			start = parser.currentIndex();
			while( !parser.endOfInput() && parser.noneOf(";") );
			String val = parser.textFrom(start).trim();

			map.put(prop,val);
			parser.match(';');
			while( matchSpace() );
		}
		return map;
	}

	private boolean matchPropertyChar() {
		return parser.inCharRange('a','z')
			|| parser.inCharRange('A','Z')
			|| parser.inCharRange('0','9')
			|| parser.anyOf("_-")
		;
	}

	private boolean matchSpace() {
		return parser.anyOf(" \t\r\n");
	}

}