listing13-10_useragentpreprocessor.java_registeringdirective

来自「着几乎所有智能机厂商都将有自己配套的App Store,甚至并非智能手机制造商的」· JAVA_REGISTERINGDIRECTIVE 代码 · 共 38 行

JAVA_REGISTERINGDIRECTIVE
38
字号
package com.apress.preprocess;

import org.apache.tools.ant.BuildException;
import de.enough.polish.preprocess.CustomPreprocessor;
import de.enough.polish.util.StringList;

public class UserAgentPreprocessor extends CustomPreprocessor {

	public UserAgentPreprocessor() {
		super();
		registerDirective( "//#useragent" );
	}

	public void processUseragent( String line, StringList lines, String className ) {
		int directiveStart = line.indexOf( "//#useragent" );
		String argument = line.substring( directiveStart
			+ "//#useragent".length() ).trim();
		int replacePos = argument.indexOf( "${useragent}" );
		if ( replacePos == -1 ) {
			throw new BuildException( getErrorStart( className, lines )
				+ "Unable to process #useragent-directive: "
				+ "found no ${useragent} sequence in line ["
				+ line + "]." );
		}
		String userAgent = this.currentDevice.getCapability( "polish.UserAgent" );
		if ( userAgent == null ) {
			userAgent = this.currentDevice.getIdentifier();
		}
		if ( this.currentLocale != null ) {
			userAgent += "<" + this.currentLocale.toString() + ">";
		}
		String result = argument.substring( 0, replacePos )
			+ userAgent
			+ argument.substring( replacePos + "${useragent}".length() );
		lines.setCurrent( result );
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?