From 3762acde6e95ae96549579cc1a9f389be320a4c3 Mon Sep 17 00:00:00 2001 From: allo Date: Sat, 6 Aug 2005 15:57:31 +0000 Subject: [PATCH] alternatives By Name git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@498 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- source/de/anomic/http/httpTemplate.java | 135 ++++++++++++++---------- 1 file changed, 80 insertions(+), 55 deletions(-) diff --git a/source/de/anomic/http/httpTemplate.java b/source/de/anomic/http/httpTemplate.java index 12e88223f..81e78c93a 100644 --- a/source/de/anomic/http/httpTemplate.java +++ b/source/de/anomic/http/httpTemplate.java @@ -252,12 +252,20 @@ public final class httpTemplate { keyStream=new ByteArrayOutputStream(); //clear + boolean byName=false; int whichPattern=0; + String patternName=""; if(pattern.containsKey(prefix + key) && pattern.get(prefix + key) != null){ try{ - whichPattern=(int)Integer.parseInt((String)pattern.get(prefix + key)); //which alternative(index) + Object tmp=pattern.get(prefix + key); //lookup by index OR Name + if(tmp instanceof String){ + byName=true; + patternName=(String)tmp;//Name + }else{ + whichPattern=(int)Integer.parseInt((String)pattern.get(prefix + key)); //index + } }catch(NumberFormatException e){ - whichPattern=0; + whichPattern=0; } }else{ //System.out.println("Pattern \""+new String(prefix + key)+"\" is not set"); //DEBUG @@ -266,59 +274,76 @@ public final class httpTemplate { int currentPattern=0; boolean found=false; keyStream = new ByteArrayOutputStream(); //reset stream - 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 - writeTemplate(pis2, out, pattern, dflt, prefix + key + "_"); - found=true; - }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested - others--; - text += "#("+keyStream.toString()+")#"; - }else{ //nested - others++; - text += "#("+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())); - writeTemplate(pis2, out, pattern, dflt, prefix + key + "_"); - - transferUntil(pis, keyStream, (new String("#(/"+key+")#")).getBytes());//to #(/key)#. - - found=true; - } - currentPattern++; - text=""; - continue; - }else{ - text += ":"; - } - } - if(!found){ - text += (char)bb; - if(pis.available()==0){ - serverLog.logError("TEMPLATE", "No Close Key found for #("+key+")#"); - found=true; - } - } - }//while + if(byName){ + //TODO: better Error Handling + transferUntil(pis, keyStream, (new String("%%"+patternName)).getBytes()); + if(pis.available()==0){ + serverLog.logError("TEMPLATE", "No such Template: %%"+patternName); + return; + } + keyStream=new ByteArrayOutputStream(); + transferUntil(pis, keyStream, "::".getBytes()); + pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toString().getBytes())); + writeTemplate(pis2, out, pattern, dflt, prefix + key + "_"); + transferUntil(pis, keyStream, (new String("#(/"+key+")#")).getBytes()); + if(pis.available()==0){ + serverLog.logError("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 + writeTemplate(pis2, out, pattern, dflt, prefix + key + "_"); + found=true; + }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested + others--; + text += "#("+keyStream.toString()+")#"; + }else{ //nested + others++; + text += "#("+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())); + writeTemplate(pis2, out, pattern, dflt, prefix + key + "_"); + + transferUntil(pis, keyStream, (new String("#(/"+key+")#")).getBytes());//to #(/key)#. + + found=true; + } + currentPattern++; + text=""; + continue; + }else{ + text += ":"; + } + } + if(!found){ + text += (char)bb; + if(pis.available()==0){ + serverLog.logError("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