Skip to Content.
Sympa Menu

perfsonar-dev - r1606 - trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest

Subject: perfsonar development work

List archive

r1606 - trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest


Chronological Thread 
  • From:
  • To:
  • Subject: r1606 - trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest
  • Date: Wed, 6 Sep 2006 06:43:09 -0400

Author: mac
Date: 2006-09-06 06:43:08 -0400 (Wed, 06 Sep 2006)
New Revision: 1606

Modified:

trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest/ExistDbHTTPAccess.java
Log:
Changes for Bug #67 and other related problems. Better parsing of
exist:result and exist:value tags

Modified:
trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest/ExistDbHTTPAccess.java
===================================================================
---
trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest/ExistDbHTTPAccess.java
2006-09-06 10:40:53 UTC (rev 1605)
+++
trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest/ExistDbHTTPAccess.java
2006-09-06 10:43:08 UTC (rev 1606)
@@ -6,6 +6,7 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.util.ArrayList;

import org.perfsonar.service.commons.storage.xmldb.XmlDbConstants;
import org.perfsonar.service.commons.storage.xmldb.XmlDbResult;
@@ -16,7 +17,7 @@
*
* @author Maciej Glowiak
*
- * $ID$
+ * $Id$
*
*/
public class ExistDbHTTPAccess implements XmlDbConstants {
@@ -141,6 +142,7 @@

//Result string
String result = null;
+ String[] resultArray = null;
int hits = 0;
int count = 0;
int first = 0;
@@ -274,7 +276,7 @@
//HTTP response available now
int responseCode = huc.getResponseCode();

- if (debug) System.out.println("[D] Response code: "+ responseCode);
+ if (debug) System.out.println("[D] Response code: "+ responseCode+"
("+huc.getResponseMessage()+")");

//if not XQuery, what means XUpdate, return empty string
if (!doXQuery) {
@@ -302,6 +304,8 @@

//read each line
StringBuffer response = new StringBuffer();
+ ArrayList<String> responseList = new ArrayList<String>();
+
String str;
while ((str = rd.readLine()) != null) {

@@ -348,6 +352,17 @@
"[RESV] count="+count);
} //if full

+ //Case if exist:result in one line

+ //if <exist:result>anything</exist:result>
+ String content =
extractXMLTag(str,"exist:result");
+ if (content!=null) {
+ //take only content
+ str=content;
+ doParseLine=true; //this time parse
line, because
+ //it may contain more data to be parsed
+ if (debug) System.out.println("[D]
Results in one line, parsing content ["+str+"]...");
+ }
+
} //<exist:result>

} else {
@@ -364,25 +379,24 @@

//parse (if inside <exist:result></exist:result>
if (doParseLine) {
-
+
if (allowRemoveExistValue) {

//remove <exist:value...
- if (str.trim().startsWith("<exist:value")) {
- int ind1 = str.indexOf('>');
- int ind2 = str.indexOf('<',ind1);
- // if negative indices,
- // it means
- // [<exist:value exist:type="(...)"/>]
- if ((ind2<0)||(ind1<0)) { str=""; } //return
empty string
- else
- str = str.substring(ind1+1,ind2);
//return content of <exist:value> tag
+ String content =
extractXMLTag(str,"exist:value");
+ if (content!=null) {
+ str=content;
+ //add to list
+ if (!("".equals(str)))
+ responseList.add(str);
+ if (debug) System.out.println("[D] Removed
<exist:value>");
}
+
}

/*
* ----------------------------------------------
- * at this stage, str contains string line
+ * at this stage, str contains string line
* ----------------------------------------------
*/

@@ -392,7 +406,7 @@
//add Line Feed sign at the end of each line
response.append("\n");

- if (debug) System.out.println("[LINE] ["+str+"]");
+ if (debug) System.out.println("[LINE] ["+str+"]");

} //parse

@@ -401,6 +415,11 @@
//convert results to String
result = response.toString();

+ //convert result list (if only exist:value) to String[]
+ if (responseList.size()>0)
+ resultArray = responseList.
+ toArray(new String[responseList.size()]);
+

} //HTTP 200 OK
else {
@@ -417,12 +436,21 @@
//return results as:

if (full) {
- return new XmlDbResult(result, hits, first, count);
- //return XmlDbResult
+
+ if (resultArray!=null)
+ return new XmlDbResult(resultArray, hits, first, count);
//return XmlDbResult
+ else
+ return new XmlDbResult(result, hits, first, count); //return
XmlDbResult
+
+
} else if (stringarray) {
+ if (resultArray!=null) {
+ return resultArray; //return String[N]
+ } else {
+ String[] resarr = {result}; //return String[1]
+ return resarr;
+ }

- String[] resarr = {result};
- return resarr; //return String[1]

} else {

@@ -433,6 +461,29 @@
} //end


+
+ protected static String extractXMLTag(String str, String tag) {
+
+ //trim from blank spaces
+ str = str.trim();
+
+ //find starting tag
+ int indexStartTag1 = str.indexOf("<"+tag);
+ if (indexStartTag1==-1) return null;
+
+ //find end of starting tag
+ int indexStartTag2 = str.indexOf(">", indexStartTag1);
+ if (indexStartTag2==-1) return null;
+
+ //find closing tag
+ int indexEndTag1 = str.indexOf("</"+tag, indexStartTag2);
+ if (indexEndTag1==-1) return null;
+
+ String substr = str.substring(indexStartTag2+1, indexEndTag1);
+ return substr;
+
+ }
+
/**
* Extracts attribute value from string
* @param a string
@@ -497,20 +548,27 @@

// --------------------------------------------------- test method

-
/**
* Test method
*/
public static void main(String[] args) throws Exception {

+ String query =
+ "declare namespace " +
+ "nmwg='http://ggf.org/ns/nmwg/base/2.0/'; " +
+ "/nmwg:store/nmwg:metadata";

// String query =
// "declare namespace " +
// "nmwg='http://ggf.org/ns/nmwg/base/2.0/'; " +
// "data(/nmwg:store/nmwg:metadata/@id)";

- //String query = "/";
+// String query = "declare namespace
nmwg=\"http://ggf.org/ns/nmwg/base/2.0/\";; "+
+//
"/nmwg:store[@type='LSStore-control']/nmwg:metadata[@id='entry-1']/nmwg:parameters"+
+//
"/nmwg:parameter[@name='timestamp']/text()";

+// String query = "/";
+
// String query = "<xu:modifications version='1.0'
xmlns:xu='http://www.xmldb.org/xupdate'
xmlns:perfsonar='http://ggf.org/ns/nmwg/tools/org/perfsonar/1.0/'
xmlns:nmwg='http://ggf.org/ns/nmwg/base/2.0/'
xmlns:psservice='http://ggf.org/ns/nmwg/tools/org/perfsonar/service/1.0/'
xmlns:nmwgt='http://ggf.org/ns/nmwg/topology/2.0/'
xmlns:netutil='http://ggf.org/ns/nmwg/characteristic/utilization/2.0/'>" +
// "<xu:append
select=\"/nmwg:store[@type='LSStore']\"
child=\"last()\">" +
// "<nmwg:data" +
@@ -533,11 +591,11 @@
// "</xu:append>" +
// "</xu:modifications>";

- String query = "<xu:modifications version='1.0'
xmlns:xu='http://www.xmldb.org/xupdate'
xmlns:nmwg='http://ggf.org/ns/nmwg/base/2.0/'>" +
- "<xu:append
select=\"/nmwg:store[@type='LSStore']\"
child=\"last()\">" +
- " <xxxxxxxxxxxxxxxxx/>" +
- "</xu:append>" +
- "</xu:modifications>";
+// String query = "<xu:modifications version='1.0'
xmlns:xu='http://www.xmldb.org/xupdate'
xmlns:nmwg='http://ggf.org/ns/nmwg/base/2.0/'>" +
+// "<xu:append
select=\"/nmwg:store[@type='LSStore']\"
child=\"last()\">" +
+// " <xxxxxxxxxxxxxxxxx/>" +
+// "</xu:append>" +
+// "</xu:modifications>";

long t0 = System.currentTimeMillis(); //----------------time 0

@@ -548,17 +606,27 @@
"sonar",
FIRST,
ALL_RESULTS,
- RETURN_XML_DB_RESULT|XUPDATE);
+ RETURN_XML_DB_RESULT|XQUERY|DEBUG_MORE);

long t1 = System.currentTimeMillis(); //----------------time 1

XmlDbResult result = (XmlDbResult)s;
- String x = result.getResultAsString();
- System.out.println("RESULT:
"+((x.length()<1000)?x:x.substring(1000)));
-
+// String x = result.getResultAsString();
+// System.out.println("RESULT:
"+((x.length()<1000)?x:x.substring(1000)));
+
+ for (int i=0; i<result.getResultAsStringArray().length; i++) {
+ String x = result.getResultAsStringArray()[i];
+ System.out.println("RESULT["+i+"]:
"+((x.length()<1000)?x:"..."+x.substring(1000)+"..."));
+ }
+
System.out.println("TIME: "+(t1-t0)+"ms");
+
+
+

}


+
+
} //ExistDbHTTPAccess



  • r1606 - trunk/perfsonar/src/org/perfsonar/service/commons/storage/xmldb/exist/rest, svnlog, 09/06/2006

Archive powered by MHonArc 2.6.16.

Top of Page