package net.traviangui;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
public class Util
{
private static Logger log = Logger.getLogger( Util.class.toString());
private static String lastStrPattern = "";
private static Pattern lastPattern = Pattern.compile( lastStrPattern);
public static String[] patternExtract( String strPattern, String str, int numFields) throws Exception
{
if( !strPattern.equals( lastStrPattern)) {
lastStrPattern = strPattern;
lastPattern = Pattern.compile( lastStrPattern);
}
Matcher m = lastPattern.matcher( str);
boolean ok = m.find();
if( !ok || m.groupCount() != numFields){
throw new Exception( "Pattern matching: pattern='" + strPattern + "', str='" + str +
"', num fields expected=" + numFields + ",num fields found= " + m.groupCount() + ", ok=" + ok);
}
String[] res = new String[numFields];
for( int i=0 ; i<numFields ; i++) {
res[i] = m.group( i+1);
}
return res;
}
public static void printTagNodes( Object[] nodes) throws Exception
{
// serialize a node to a file, output stream, DOM, JDom...
System.out.println( "\n\n\nINICIO");
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties props = cleaner.getProperties();
PrettyXmlSerializer serializer = new PrettyXmlSerializer( props);
for( int i=0 ; i<nodes.length ; i++) {
System.out.println( "######################## Va uno " + i);
OutputStream oStream = new ByteArrayOutputStream( );
if( nodes[i] instanceof TagNode) {
serializer.writeXmlToStream( (TagNode)nodes[i], oStream);
}
else {
oStream.write( nodes[i].toString().getBytes());
}
System.out.println( oStream);
}
System.out.println( "FIN");
}
static public boolean obsolete( Date timestamp)
{
return (new Date( ).getTime() - timestamp.getTime()) > 5 * 60 * 1000; // 5 minutos
}
}