/*******************************************************************************
* Copyright (c) 2008, 2009 Innoopract Informationssysteme GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innoopract Informationssysteme GmbH - initial API and implementation
* EclipseSource - ongoing development
******************************************************************************/
package org.eclipse.rwt.internal.theme;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class AppearancesUtil {
private static final Pattern END_TEMPLATE_PATTERN
= Pattern.compile( "(\\r|\\n).*?END TEMPLATE" );
private static final Pattern BEGIN_TEMPLATE_PATTERN
= Pattern.compile( "BEGIN TEMPLATE.*(\\r|\\n)" );
private AppearancesUtil() {
// prevent instantiation
}
public static String readAppearanceFile( final InputStream inStream )
throws IOException
{
StringBuffer sb = new StringBuffer();
InputStreamReader reader = new InputStreamReader( inStream, "UTF-8" );
BufferedReader br = new BufferedReader( reader );
for( int i = 0; i < 100; i++ ) {
int character = br.read();
while( character != -1 ) {
sb.append( ( char )character );
character = br.read();
}
}
return stripTemplate( sb.toString() );
}
private static String stripTemplate( final String input ) {
int beginIndex = 0;
int endIndex = input.length();
Matcher matcher;
matcher = BEGIN_TEMPLATE_PATTERN.matcher( input );
if( matcher.find() ) {
beginIndex = matcher.end();
}
matcher = END_TEMPLATE_PATTERN.matcher( input );
if( matcher.find() ) {
endIndex = matcher.start();
}
return input.substring( beginIndex, endIndex );
}
}