From ef751b9d3397578d494ae7998cc085df835573b1 Mon Sep 17 00:00:00 2001 From: theli Date: Wed, 13 Sep 2006 13:56:10 +0000 Subject: [PATCH] *) removing all string operations from the template engine - engine should fully operate on bytes now git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2567 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/http/httpTemplate.java | 623 +++++++++++++----------- 1 file changed, 339 insertions(+), 284 deletions(-) diff --git a/source/de/anomic/http/httpTemplate.java b/source/de/anomic/http/httpTemplate.java index 26ef5ddac..00ff87713 100644 --- a/source/de/anomic/http/httpTemplate.java +++ b/source/de/anomic/http/httpTemplate.java @@ -1,48 +1,48 @@ -// httpTemplate.java -// ------------------------------------- -// (C) by Michael Peter Christen; mc@anomic.de -// first published on http://www.anomic.de -// Frankfurt, Germany, 2004 -// last major change: 16.01.2005 -// -// $LastChangedDate$ -// $LastChangedRevision$ -// $LastChangedBy$ -// -// extended for multi- and alternatives-templates by Alexander Schier -// -// This program is free software; you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation; either version 2 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// Using this software in any meaning (reading, learning, copying, compiling, -// running) means that you agree that the Author(s) is (are) not responsible -// for cost, loss of data or any harm that may be caused directly or indirectly -// by usage of this softare or this documentation. The usage of this software -// is on your own risk. The installation and usage (starting/running) of this -// software may allow other people or application to access your computer and -// any attached devices and is highly dependent on the configuration of the -// software which must be done by the user of the software; the author(s) is -// (are) also not responsible for proper configuration and usage of the -// software, even if provoked by documentation provided together with -// the software. -// -// Any changes to this file according to the GPL as documented in the file -// gpl.txt aside this file in the shipment you received can be done to the -// lines that follows this copyright notice here, but changes must not be -// done inside the copyright notice above. A re-distribution must contain -// the intact and unchanged copyright notice. -// Contributions and changes to the program code must be marked as such. +//httpTemplate.java +//------------------------------------- +//(C) by Michael Peter Christen; mc@anomic.de +//first published on http://www.anomic.de +//Frankfurt, Germany, 2004 +//last major change: 16.01.2005 + +//$LastChangedDate$ +//$LastChangedRevision$ +//$LastChangedBy$ + +//extended for multi- and alternatives-templates by Alexander Schier + +//This program is free software; you can redistribute it and/or modify +//it under the terms of the GNU General Public License as published by +//the Free Software Foundation; either version 2 of the License, or +//(at your option) any later version. + +//This program is distributed in the hope that it will be useful, +//but WITHOUT ANY WARRANTY; without even the implied warranty of +//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//GNU General Public License for more details. + +//You should have received a copy of the GNU General Public License +//along with this program; if not, write to the Free Software +//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +//Using this software in any meaning (reading, learning, copying, compiling, +//running) means that you agree that the Author(s) is (are) not responsible +//for cost, loss of data or any harm that may be caused directly or indirectly +//by usage of this softare or this documentation. The usage of this software +//is on your own risk. The installation and usage (starting/running) of this +//software may allow other people or application to access your computer and +//any attached devices and is highly dependent on the configuration of the +//software which must be done by the user of the software; the author(s) is +//(are) also not responsible for proper configuration and usage of the +//software, even if provoked by documentation provided together with +//the software. + +//Any changes to this file according to the GPL as documented in the file +//gpl.txt aside this file in the shipment you received can be done to the +//lines that follows this copyright notice here, but changes must not be +//done inside the copyright notice above. A re-distribution must contain +//the intact and unchanged copyright notice. +//Contributions and changes to the program code must be marked as such. package de.anomic.http; @@ -56,6 +56,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PushbackInputStream; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.Hashtable; @@ -121,7 +122,7 @@ import de.anomic.server.logging.serverLog; * would be adressed as "alternative_repeat_"+number+"_test" */ public final class httpTemplate { - + public static final byte hash = (byte)'#'; private static final byte[] hasha = {hash}; @@ -147,12 +148,14 @@ public final class httpTemplate { private static final byte[] iOpen = {hash, ps}; private static final byte[] iClose = {ps, hash}; + public static final byte[] slash = {(byte)'/'}; + public static final Object[] meta_quotation = new Object[] { new Object[] {pOpen, pClose}, new Object[] {mOpen, mClose}, new Object[] {aOpen, aClose}, new Object[] {iOpen, iClose} - }; + }; public static serverByteBuffer[] splitQuotations(serverByteBuffer text) { List l = splitQuotation(text, 0); @@ -160,7 +163,7 @@ public final class httpTemplate { for (int i = 0; i < l.size(); i++) sbbs[i] = (serverByteBuffer) l.get(i); return sbbs; } - + public static List splitQuotation(serverByteBuffer text, int qoff) { ArrayList l = new ArrayList(); if (qoff >= meta_quotation.length) { @@ -185,7 +188,7 @@ public final class httpTemplate { text.clear(); } } - + // find double-points while ((text.length() > 0) && ((p = text.indexOf(dpdpa)) >= 0)) { l.addAll(splitQuotation(new serverByteBuffer(text.getBytes(0, p)), qoff)); @@ -193,16 +196,16 @@ public final class httpTemplate { l.addAll(splitQuotation(new serverByteBuffer(text.getBytes(p + 2)), qoff)); text.clear(); } - + // add remaining if (text.length() > 0) l.addAll(splitQuotation(text, qoff)); return l; } - - /** - * transfer until a specified pattern is found; everything but the pattern is transfered so far - * the function returns true, if the pattern is found - */ + + /** + * transfer until a specified pattern is found; everything but the pattern is transfered so far + * the function returns true, if the pattern is found + */ private static boolean transferUntil(PushbackInputStream i, OutputStream o, byte[] pattern) throws IOException { int b, bb; boolean equal; @@ -225,252 +228,263 @@ public final class httpTemplate { } return false; } - - public static String writeTemplate(InputStream in, OutputStream out, Hashtable pattern, byte[] dflt) throws IOException { - return writeTemplate(in, out, pattern, dflt, ""); + + public static byte[] writeTemplate(InputStream in, OutputStream out, Hashtable pattern, byte[] dflt) throws IOException { + return writeTemplate(in, out, pattern, dflt, new byte[0]); } - /** - * Reads a input stream, and writes the data with replaced templates on a output stream - */ - public static String writeTemplate(InputStream in, OutputStream out, Hashtable pattern, byte[] dflt, String prefix) throws IOException { + /** + * Reads a input stream, and writes the data with replaced templates on a output stream + */ + public static byte[] writeTemplate(InputStream in, OutputStream out, Hashtable pattern, byte[] dflt, byte[] prefix) throws IOException { PushbackInputStream pis = new PushbackInputStream(in, 100); ByteArrayOutputStream keyStream; - String key; - String multi_key; + byte[] key; + byte[] multi_key; byte[] replacement; int bb; - StringBuffer structure=new StringBuffer(); - + serverByteBuffer structure=new serverByteBuffer(); + while (transferUntil(pis, out, hasha)) { - bb = pis.read(); - keyStream = new ByteArrayOutputStream(); - - if( (bb & 0xFF) == lcbr ){ //multi - if( transferUntil(pis, keyStream, mClose) ){ //close tag - //multi_key = "_" + keyStream.toString(); //for _Key - bb = pis.read(); - if( (bb & 0xFF) != 10){ //kill newline - pis.unread(bb); - } - multi_key = keyStream.toString(); //IMPORTANT: no prefix here - keyStream = new ByteArrayOutputStream(); //reset stream - - /* DEBUG - print key + value + bb = pis.read(); + keyStream = new ByteArrayOutputStream(); + + if( (bb & 0xFF) == lcbr ){ //multi + if( transferUntil(pis, keyStream, mClose) ){ //close tag + //multi_key = "_" + keyStream.toString(); //for _Key + bb = pis.read(); + if( (bb & 0xFF) != 10){ //kill newline + pis.unread(bb); + } + multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here + keyStream = new ByteArrayOutputStream(); //reset stream + + /* DEBUG - print key + value try{ System.out.println("Key: "+prefix+multi_key+ "; Value: "+pattern.get(prefix+multi_key)); }catch(NullPointerException e){ System.out.println("Key: "+prefix+multi_key); } - */ - - //this needs multi_key without prefix - if( transferUntil(pis, keyStream, (new String(mOpen) + "/" + multi_key + new String(mClose)).getBytes()) ){ - bb = pis.read(); - if((bb & 0xFF) != 10){ //kill newline - pis.unread(bb); - } + */ + + //this needs multi_key without prefix + if( transferUntil(pis, keyStream, appendBytes(mOpen,slash,multi_key,mClose))){ + bb = pis.read(); + if((bb & 0xFF) != 10){ //kill newline + pis.unread(bb); + } + + byte[] text=keyStream.toByteArray(); //text between #{key}# an #{/key}# + int num=0; + String patternKey = getPatternKey(prefix, multi_key); + if(pattern.containsKey(patternKey) && pattern.get(patternKey) != null){ + try{ + num=Integer.parseInt((String)pattern.get(patternKey)); // Key contains the iteration number as string + }catch(NumberFormatException e){ + num=0; + } + //System.out.println(multi_key + ": " + num); //DEBUG + }else{ + //0 interations - no display + //System.out.println("_"+new String(multi_key)+" is null or does not exist"); //DEBUG + } - String text=keyStream.toString(); //text between #{key}# an #{/key}# - int num=0; - if(pattern.containsKey(prefix+multi_key) && pattern.get(prefix+multi_key) != null){ - try{ - num=Integer.parseInt((String)pattern.get(prefix+multi_key)); // Key contains the iteration number as string - }catch(NumberFormatException e){ - num=0; - } - //System.out.println(multi_key + ": " + num); //DEBUG - }else{ - //0 interations - no display - //System.out.println("_"+new String(multi_key)+" is null or does not exist"); //DEBUG - } - //Enumeration enx = pattern.keys(); while (enx.hasMoreElements()) System.out.println("KEY=" + enx.nextElement()); // DEBUG - structure.append("<"+multi_key+" type=\"multi\" num=\""+num+"\">\n"); - for(int i=0;i < num;i++ ){ - PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); - //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG - //System.out.println(text); - structure.append(writeTemplate(pis2, out, pattern, dflt, prefix+multi_key + "_" + i + "_")); - }//for - structure.append("\n"); - }else{//transferUntil - serverLog.logSevere("TEMPLATE", "No Close Key found for #{"+multi_key+"}#"); //prefix here? - } - } - }else if( (bb & 0xFF) == lrbr ){ //alternatives - int others=0; - serverByteBuffer text= new serverByteBuffer(); - PushbackInputStream pis2; - - transferUntil(pis, keyStream, aClose); - key = keyStream.toString(); //Caution: Key does not contain prefix - - /* DEBUG - print key + value + structure.append("<".getBytes("UTF-8")) + .append(multi_key) + .append(" type=\"multi\" num=\"".getBytes("UTF-8")) + .append(Integer.toString(num).getBytes("UTF-8")) + .append("\">\n".getBytes("UTF-8")); + for(int i=0;i < num;i++ ){ + PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text)); + //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG + //System.out.println(text); + structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i))); + }//for + structure.append("\n".getBytes("UTF-8")); + }else{//transferUntil + serverLog.logSevere("TEMPLATE", "No Close Key found for #{"+multi_key+"}#"); //prefix here? + } + } + }else if( (bb & 0xFF) == lrbr ){ //alternatives + int others=0; + serverByteBuffer text= new serverByteBuffer(); + PushbackInputStream pis2; + + transferUntil(pis, keyStream, aClose); + key = keyStream.toByteArray(); //Caution: Key does not contain prefix + + /* DEBUG - print key + value try{ System.out.println("Key: "+prefix+key+ "; Value: "+pattern.get(prefix+key)); }catch(NullPointerException e){ System.out.println("Key: "+prefix+key); } - */ - - keyStream=new ByteArrayOutputStream(); //clear - - boolean byName=false; - int whichPattern=0; - String patternName=""; - if(pattern.containsKey(prefix + key) && pattern.get(prefix + key) != null){ - String patternId=(String)pattern.get(prefix + key); - try{ - whichPattern=Integer.parseInt(patternId); //index - }catch(NumberFormatException e){ - whichPattern=0; - byName=true; - patternName=patternId; - } - }else{ - //System.out.println("Pattern \""+new String(prefix + key)+"\" is not set"); //DEBUG - } - - int currentPattern=0; - boolean found=false; - keyStream = new ByteArrayOutputStream(); //reset stream - if(byName){ - //TODO: better Error Handling - transferUntil(pis, keyStream, (new String("%%"+patternName)).getBytes()); - if(pis.available()==0){ - serverLog.logSevere("TEMPLATE", "No such Template: %%"+patternName); - return structure.toString(); - } - keyStream=new ByteArrayOutputStream(); - transferUntil(pis, keyStream, "::".getBytes()); - pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toString().getBytes())); - structure.append(writeTemplate(pis2, out, pattern, dflt, prefix + key + "_")); - transferUntil(pis, keyStream, (new String("#(/"+key+")#")).getBytes()); - if(pis.available()==0){ - serverLog.logSevere("TEMPLATE", "No Close Key found for #("+key+")# (by Name)"); - } - }else{ - while(!found){ - bb=pis.read(); - if( (bb & 0xFF) == hash){ - bb=pis.read(); - if( (bb & 0xFF) == lrbr){ - transferUntil(pis, keyStream, aClose); - - //reached the end. output last string. - if(keyStream.toString().equals("/" + key)){ - pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); - //this maybe the wrong, but its the last - structure.append("<"+key+" type=\"alternative\" which=\""+whichPattern+"\" found=\"0\">\n"); - structure.append(writeTemplate(pis2, out, pattern, dflt, prefix + key + "_")); - structure.append("\n"); - found=true; - }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested - others--; - text.append("#("+keyStream.toString()+")#"); - }else{ //nested - others++; - text.append("#("+keyStream.toString()+")#"); - } - keyStream = new ByteArrayOutputStream(); //reset stream - continue; - }else{ //is not #( - pis.unread(bb);//is processed in next loop - bb = (hash);//will be added to text this loop - //text += "#"; - } - }else if( (bb & 0xFF) == ':' && others==0){//ignore :: in nested Expressions - bb=pis.read(); - if( (bb & 0xFF) == ':'){ - if(currentPattern == whichPattern){ //found the pattern - pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); - structure.append("<"+key+" type=\"alternative\" which=\""+whichPattern+"\" found=\"0\">\n"); - structure.append(writeTemplate(pis2, out, pattern, dflt, prefix + key + "_")); - structure.append("\n"); - - transferUntil(pis, keyStream, (new String("#(/"+key+")#")).getBytes());//to #(/key)#. - - found=true; - } - currentPattern++; - text.clear(); - continue; - }else{ - text.append(":"); - } - } - if(!found){ - text.append((byte)bb); - if(pis.available()==0){ - serverLog.logSevere("TEMPLATE", "No Close Key found for #("+key+")# (by Index)"); - found=true; - } - } - }//while - }//if(byName) (else branch) - }else if( (bb & 0xFF) == lbr ){ //normal - if (transferUntil(pis, keyStream, pClose)) { - // pattern detected, write replacement - key = keyStream.toString(); - replacement = replacePattern(prefix+key, pattern, dflt); //replace - structure.append("<"+key+" type=\"normal\">\n"); - structure.append(new String(replacement)); - structure.append("\n"); - - /* DEBUG + */ + + keyStream=new ByteArrayOutputStream(); //clear + + boolean byName=false; + int whichPattern=0; + byte[] patternName = new byte[0]; + String patternKey = getPatternKey(prefix, key); + if(pattern.containsKey(patternKey) && pattern.get(patternKey) != null){ + String patternId=(String)pattern.get(patternKey); + try{ + whichPattern=Integer.parseInt(patternId); //index + }catch(NumberFormatException e){ + whichPattern=0; + byName=true; + patternName=patternId.getBytes("UTF-8"); + } + }else{ + //System.out.println("Pattern \""+new String(prefix + key)+"\" is not set"); //DEBUG + } + + int currentPattern=0; + boolean found=false; + keyStream = new ByteArrayOutputStream(); //reset stream + if(byName){ + //TODO: better Error Handling + transferUntil(pis, keyStream,appendBytes("%%".getBytes("UTF-8"),patternName,null,null)); + if(pis.available()==0){ + serverLog.logSevere("TEMPLATE", "No such Template: %%"+patternName); + return structure.getBytes(); + } + keyStream=new ByteArrayOutputStream(); + transferUntil(pis, keyStream, "::".getBytes()); + pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray())); + structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); + transferUntil(pis, keyStream, appendBytes("#(/".getBytes("UTF-8"),key,")#".getBytes("UTF-8"),null)); + if(pis.available()==0){ + serverLog.logSevere("TEMPLATE", "No Close Key found for #("+key+")# (by Name)"); + } + }else{ + while(!found){ + bb=pis.read(); + if( (bb & 0xFF) == hash){ + bb=pis.read(); + if( (bb & 0xFF) == lrbr){ + transferUntil(pis, keyStream, aClose); + + //reached the end. output last string. + if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slash, key, null,null))) { + pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); + //this maybe the wrong, but its the last + structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"alternative\" which=\"".getBytes("UTF-8")).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes("UTF-8")); + structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); + structure.append("\n".getBytes("UTF-8")); + found=true; + }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested + others--; + text.append("#(".getBytes("UTF-8")).append(keyStream.toByteArray()).append(")#".getBytes("UTF-8")); + }else{ //nested + others++; + text.append("#(".getBytes("UTF-8")).append(keyStream.toByteArray()).append(")#".getBytes("UTF-8")); + } + keyStream = new ByteArrayOutputStream(); //reset stream + continue; + }else{ //is not #( + pis.unread(bb);//is processed in next loop + bb = (hash);//will be added to text this loop + //text += "#"; + } + }else if( (bb & 0xFF) == ':' && others==0){//ignore :: in nested Expressions + bb=pis.read(); + if( (bb & 0xFF) == ':'){ + if(currentPattern == whichPattern){ //found the pattern + pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); + structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"alternative\" which=\"".getBytes("UTF-8")).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes("UTF-8")); + structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); + structure.append("\n".getBytes("UTF-8")); + + transferUntil(pis, keyStream, appendBytes("#(/".getBytes("UTF-8"),key,")#".getBytes("UTF-8"),null));//to #(/key)#. + + found=true; + } + currentPattern++; + text.clear(); + continue; + }else{ + text.append(":".getBytes("UTF-8")); + } + } + if(!found){ + text.append((byte)bb); + if(pis.available()==0){ + serverLog.logSevere("TEMPLATE", "No Close Key found for #("+key+")# (by Index)"); + found=true; + } + } + }//while + }//if(byName) (else branch) + }else if( (bb & 0xFF) == lbr ){ //normal + if (transferUntil(pis, keyStream, pClose)) { + // pattern detected, write replacement + key = keyStream.toByteArray(); + String patternKey = getPatternKey(prefix, key); + replacement = replacePattern(patternKey, pattern, dflt); //replace + structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"normal\">\n".getBytes("UTF-8")); + structure.append(replacement); + structure.append("\n".getBytes("UTF-8")); + + /* DEBUG try{ System.out.println("Key: "+key+ "; Value: "+pattern.get(key)); }catch(NullPointerException e){ System.out.println("Key: "+key); } - */ + */ - serverFileUtils.write(replacement, out); - } else { - // inconsistency, simply finalize this - serverFileUtils.copy(pis, out); - return structure.toString(); - } - }else if( (bb & 0xFF) == ps){ //include - String include = ""; - String line = ""; - keyStream = new ByteArrayOutputStream(); //reset stream - if(transferUntil(pis, keyStream, iClose)){ - String filename = keyStream.toString(); - if(filename.startsWith( Character.toString((char)lbr) ) && filename.endsWith( Character.toString((char)rbr) )){ //simple pattern for filename - filename= new String(replacePattern( prefix + filename.substring(1, filename.length()-1), pattern, dflt)); - } - if (!filename.equals("") && !java.util.Arrays.equals(filename.getBytes(), dflt)) { - BufferedReader br = null; - try{ - //br = new BufferedReader(new InputStreamReader(new FileInputStream( filename ))); //Simple Include - br = new BufferedReader( new InputStreamReader(new FileInputStream( httpdFileHandler.getLocalizedFile(filename) )) ); //YaCy (with Locales) - //Read the Include - while( (line = br.readLine()) != null ){ - include+=line+de.anomic.server.serverCore.crlfString; - } - }catch(IOException e){ - //file not found? - serverLog.logSevere("FILEHANDLER","Include Error with file: "+filename); - } finally { - if (br!=null) try{br.close(); br=null;}catch(Exception e){} + serverFileUtils.write(replacement, out); + } else { + // inconsistency, simply finalize this + serverFileUtils.copy(pis, out); + return structure.getBytes(); } - PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes())); - structure.append("\n"); - structure.append(writeTemplate(pis2, out, pattern, dflt, prefix)); - structure.append("\n"); - } + }else if( (bb & 0xFF) == ps){ //include + serverByteBuffer include = new serverByteBuffer(); + keyStream = new ByteArrayOutputStream(); //reset stream + if(transferUntil(pis, keyStream, iClose)){ + byte[] filename = keyStream.toByteArray(); + //if(filename.startsWith( Character.toString((char)lbr) ) && filename.endsWith( Character.toString((char)rbr) )){ //simple pattern for filename + if((filename[0] == lbr) && (filename[filename.length-1] == rbr)){ //simple pattern for filename + byte[] newFilename = new byte[filename.length-2]; + System.arraycopy(filename, 1, newFilename, 0, newFilename.length); + String patternkey = getPatternKey(prefix, newFilename); + filename= replacePattern(patternkey, pattern, dflt); + } + if (filename.length > 0 && !java.util.Arrays.equals(filename, dflt)) { + BufferedReader br = null; + try{ + //br = new BufferedReader(new InputStreamReader(new FileInputStream( filename ))); //Simple Include + br = new BufferedReader( new InputStreamReader(new FileInputStream( httpdFileHandler.getLocalizedFile(new String(filename,"UTF-8"))),"UTF-8") ); //YaCy (with Locales) + //Read the Include + String line = ""; + while( (line = br.readLine()) != null ){ + include.append(line.getBytes("UTF-8")).append(de.anomic.server.serverCore.crlfString.getBytes("UTF-8")); + } + }catch(IOException e){ + //file not found? + serverLog.logSevere("FILEHANDLER","Include Error with file: "+filename); + } finally { + if (br!=null) try{br.close(); br=null;}catch(Exception e){} + } + PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes())); + structure.append("\n".getBytes("UTF-8")); + structure.append(writeTemplate(pis2, out, pattern, dflt, prefix)); + structure.append("\n".getBytes("UTF-8")); + } + } + }else{ //no match, but a single hash (output # + bb) + byte[] tmp=new byte[2]; + tmp[0]=hash; + tmp[1]=(byte)bb; + serverFileUtils.write(tmp, out); } - }else{ //no match, but a single hash (output # + bb) - byte[] tmp=new byte[2]; - tmp[0]=hash; - tmp[1]=(byte)bb; - serverFileUtils.write(tmp, out); - } } //System.out.println(structure.toString()); //DEBUG - return structure.toString(); + return structure.getBytes(); } public static byte[] replacePattern(String key, Hashtable pattern, byte dflt[]) { @@ -479,18 +493,18 @@ public final class httpTemplate { if (pattern.containsKey(key)) { value = pattern.get(key); //try { - if (value instanceof byte[]) { - replacement = (byte[]) value; - } else if (value instanceof String) { - replacement = ((String) value).getBytes(); - //replacement = ((String) value).getBytes("UTF-8"); - } else { - replacement = value.toString().getBytes(); - //replacement = value.toString().getBytes("UTF-8"); - } -// } catch (UnsupportedEncodingException e) { -// replacement = dflt; -// } + if (value instanceof byte[]) { + replacement = (byte[]) value; + } else if (value instanceof String) { + replacement = ((String) value).getBytes(); + //replacement = ((String) value).getBytes("UTF-8"); + } else { + replacement = value.toString().getBytes(); + //replacement = value.toString().getBytes("UTF-8"); + } +// } catch (UnsupportedEncodingException e) { +// replacement = dflt; +// } } else { replacement = dflt; } @@ -530,5 +544,46 @@ public final class httpTemplate { } return result; } + + public static byte[] newPrefix(byte[] oldPrefix, byte[] key) { + serverByteBuffer newPrefix = new serverByteBuffer(); + newPrefix.append(oldPrefix) + .append(key) + .append("_".getBytes()); + return newPrefix.getBytes(); + } + + public static byte[] newPrefix(byte[] oldPrefix, byte[] multi_key, int i) { + serverByteBuffer newPrefix = new serverByteBuffer(); + try { + newPrefix.append(oldPrefix) + .append(multi_key) + .append("_".getBytes()) + .append(Integer.toString(i).getBytes("UTF-8")) + .append("_".getBytes()); + + } catch (UnsupportedEncodingException e) {} + return newPrefix.getBytes(); + } + + public static String getPatternKey(byte[] prefix, byte[] key) { + serverByteBuffer patternKey = new serverByteBuffer(); + patternKey.append(prefix) + .append(key); + try { + return new String(patternKey.getBytes(),"UTF-8"); + } catch (UnsupportedEncodingException e) { + return null; + } + } + + public static byte[] appendBytes(byte[] b1, byte[] b2, byte[] b3, byte[] b4) { + serverByteBuffer byteArray = new serverByteBuffer(); + byteArray.append(b1) + .append(b2); + if (b3 != null) byteArray.append(b3); + if (b4 != null) byteArray.append(b4); + return byteArray.getBytes(); + } }