Removed dependency on xerces

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/application-support-layer/applicationSupportLayerCore@96733 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
nikolas.laskaris 2014-06-05 10:03:56 +00:00
parent c103b97609
commit a68ed9e98b
3 changed files with 150 additions and 133 deletions

View File

@ -71,13 +71,14 @@
<version>1.6.4</version>
<scope>test</scope>
</dependency>
<!--
should be removed after release 3.2.0
<dependency>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
<version>[2.0.0, 3.0.0]</version>
<version>[2.0.0, 3.0.0)</version>
</dependency>
-->
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
@ -97,6 +98,7 @@
<version>2.3.0</version>
</dependency>
<!--
should be removed after release 3.2.0
<dependency>
<groupId>net.sourceforge.addressing</groupId>
<artifactId>addressing</artifactId>

View File

@ -33,6 +33,7 @@ import org.gcube.informationsystem.publisher.stubs.registry.faults.PublisherExce
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.slf4j.Logger;
@ -89,7 +90,6 @@ public class GenericResource implements GenericResourceInfoI {
try {
client = clientFor(org.gcube.common.resources.gcore.GenericResource.class); //GHNContext.getImplementation(ISClient.class);
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("Exception:", e);
client = null;
}
@ -158,19 +158,27 @@ public class GenericResource implements GenericResourceInfoI {
SimpleQuery query = null;
try {
query = queryFor(org.gcube.common.resources.gcore.GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType eq 'DataSource'");
query.addCondition("$resource/Profile/SecondaryType eq 'DataSource'"); //this brings all collections, need to filter out the opensearch ones.
if(onlyUserCollections)
query.addCondition("$resource/Profile/Body/SourceProperties/user eq 'true'");
List<org.gcube.common.resources.gcore.GenericResource> results = client.submit(query);
if (results == null || results.size() == 0)
logger.debug("Couldn't find any tree collections within that scope! Will return empty list.");
else
logger.debug("# of Tree Collections found: "+ results.size());
for (org.gcube.common.resources.gcore.GenericResource gr : results)
pairs.put(gr.id(), gr);
} catch (Exception e) {
logger.debug("Remote Exception:" + e.toString());
}
//remove from all collections set, the opensearch ones !
for(String key : pairs.keySet()){
org.gcube.common.resources.gcore.GenericResource collection = pairs.get(key);
Element body = collection.profile().body();
if(body.getElementsByTagName("type").getLength()==0)
pairs.remove(key);
else
logger.debug("Found tree collection: "+key);
}
logger.debug("# of Tree Collections found: "+ pairs.size());
return pairs;
}
@ -185,7 +193,9 @@ public class GenericResource implements GenericResourceInfoI {
SimpleQuery query = null;
try {
query = queryFor(org.gcube.common.resources.gcore.GenericResource.class);
query.addCondition("$resource/Profile/SecondaryType eq 'GCUBECollection'");
// query.addCondition("$resource/Profile/SecondaryType eq 'GCUBECollection'");
query.addCondition("$resource/Profile/SecondaryType eq 'DataSource'"); //changed from GCUBECollection to DataSource (2 be same as the tree collections)
query.addCondition("$resource/Profile/Body/SourceProperties/type eq 'opensearch'");
if(onlyUserCollections)
query.addCondition("$resource/Profile/Body/CollectionInfo/user eq 'true'");
List<org.gcube.common.resources.gcore.GenericResource> results = client.submit(query);

View File

@ -1,127 +1,132 @@
package org.gcube.application.framework.core.util;
///////
// should be removed after release 3.2.0
//////
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Valia Tsagkalidou (NKUA)
* @author Nikolas Laskaris (NKUA)
*
*/
public class TransformXSLT {
/** The logger. */
private static final Logger logger = LoggerFactory.getLogger(TransformXSLT.class);
/**
* Transforms an xml document based on the given xslt
* @param xslt the xslt for transforming the xml
* @param xml the xml to be transformed
* @return a string containing the transformed xml (output of the transformation)
*/
public static String transform(String xslt, String xml)
{
Transformer transformer;
try
{//Retrieve the XSLT from the DIS (generic resource), and create the transformer
ByteArrayInputStream xsltStream = new ByteArrayInputStream(xslt.getBytes());
TransformerFactory tFactory = TransformerFactory.newInstance();
transformer = tFactory.newTransformer(new StreamSource(xsltStream));
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
Document doc = null;
doc = dfactory.newDocumentBuilder().parse(xml);
// Apply the transformation
ByteArrayOutputStream ba_stream = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat(doc);
format.setIndenting(false);
format.setOmitDocumentType(true);
format.setOmitXMLDeclaration(true);
StringWriter writer = new StringWriter();
XMLSerializer serial = new XMLSerializer(writer,format);
serial.serialize(doc);
transformer.transform(new StreamSource(new ByteArrayInputStream(writer.toString().getBytes())), new StreamResult(ba_stream));
//Prepares the object to be returned
StringBuffer buffer = new StringBuffer();
try {
InputStreamReader isr = new InputStreamReader( new ByteArrayInputStream(ba_stream.toByteArray()),
"UTF8");
Reader in2 = new BufferedReader(isr);
int ch;
while ((ch = in2.read()) > -1) {
buffer.append((char)ch);
}
in2.close();
return buffer.toString();
} catch (Exception e) {
logger.error("Exception:", e);
}
}
catch (Exception e) {
logger.error("Exception:", e);
}
return null;
}
/**
* Transforms an xml document based on the given transformer
* @param transformer the transformer based on which the transformation will be applied
* @param xml the xml document to be transformed
* @return a string containing the transformed xml (output of the transformation)
*/
public static String transform(Transformer transformer, String xml)
{
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
Document doc = null;
try
{
doc = dfactory.newDocumentBuilder().parse(xml);
ByteArrayOutputStream ba_stream = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat(doc);
format.setIndenting(false);
format.setOmitDocumentType(true);
format.setOmitXMLDeclaration(true);
StringWriter writer = new StringWriter();
XMLSerializer serial = new XMLSerializer(writer,format);
serial.serialize(doc);
transformer.transform(new StreamSource(new ByteArrayInputStream(writer.toString().getBytes())), new StreamResult(ba_stream));
//Prepares the object to be returned
StringBuffer buffer = new StringBuffer();
try {
InputStreamReader isr = new InputStreamReader( new ByteArrayInputStream(ba_stream.toByteArray()),
"UTF8");
Reader in2 = new BufferedReader(isr);
int ch;
while ((ch = in2.read()) > -1) {
buffer.append((char)ch);
}
in2.close();
return buffer.toString();
} catch (Exception e) {
logger.error("Exception:", e);
}
}
catch (Exception e) {
logger.error("Exception:", e);
}
return null;
}
}
//package org.gcube.application.framework.core.util;
//
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.InputStreamReader;
//import java.io.Reader;
//import java.io.StringWriter;
//
//import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.stream.StreamResult;
//import javax.xml.transform.stream.StreamSource;
//
//import org.apache.xml.serialize.OutputFormat;
//import org.apache.xml.serialize.XMLSerializer;
//import org.w3c.dom.Document;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
///**
// * @author Valia Tsagkalidou (NKUA)
// * @author Nikolas Laskaris (NKUA)
// *
// */
//public class TransformXSLT {
//
// /** The logger. */
// private static final Logger logger = LoggerFactory.getLogger(TransformXSLT.class);
//
// /**
// * Transforms an xml document based on the given xslt
// * @param xslt the xslt for transforming the xml
// * @param xml the xml to be transformed
// * @return a string containing the transformed xml (output of the transformation)
// */
// public static String transform(String xslt, String xml)
// {
// Transformer transformer;
// try
// {//Retrieve the XSLT from the DIS (generic resource), and create the transformer
// ByteArrayInputStream xsltStream = new ByteArrayInputStream(xslt.getBytes());
// TransformerFactory tFactory = TransformerFactory.newInstance();
// transformer = tFactory.newTransformer(new StreamSource(xsltStream));
//
// DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
// Document doc = null;
//
// doc = dfactory.newDocumentBuilder().parse(xml);
// // Apply the transformation
// ByteArrayOutputStream ba_stream = new ByteArrayOutputStream();
// OutputFormat format = new OutputFormat(doc);
// format.setIndenting(false);
// format.setOmitDocumentType(true);
// format.setOmitXMLDeclaration(true);
// StringWriter writer = new StringWriter();
// XMLSerializer serial = new XMLSerializer(writer,format);
// serial.serialize(doc);
// transformer.transform(new StreamSource(new ByteArrayInputStream(writer.toString().getBytes())), new StreamResult(ba_stream));
// //Prepares the object to be returned
// StringBuffer buffer = new StringBuffer();
// try {
// InputStreamReader isr = new InputStreamReader( new ByteArrayInputStream(ba_stream.toByteArray()),
// "UTF8");
// Reader in2 = new BufferedReader(isr);
// int ch;
// while ((ch = in2.read()) > -1) {
// buffer.append((char)ch);
// }
// in2.close();
// return buffer.toString();
// } catch (Exception e) {
// logger.error("Exception:", e);
// }
// }
// catch (Exception e) {
// logger.error("Exception:", e);
// }
// return null;
// }
//
// /**
// * Transforms an xml document based on the given transformer
// * @param transformer the transformer based on which the transformation will be applied
// * @param xml the xml document to be transformed
// * @return a string containing the transformed xml (output of the transformation)
// */
// public static String transform(Transformer transformer, String xml)
// {
// DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
// Document doc = null;
//
// try
// {
// doc = dfactory.newDocumentBuilder().parse(xml);
// ByteArrayOutputStream ba_stream = new ByteArrayOutputStream();
// OutputFormat format = new OutputFormat(doc);
// format.setIndenting(false);
// format.setOmitDocumentType(true);
// format.setOmitXMLDeclaration(true);
// StringWriter writer = new StringWriter();
// XMLSerializer serial = new XMLSerializer(writer,format);
// serial.serialize(doc);
// transformer.transform(new StreamSource(new ByteArrayInputStream(writer.toString().getBytes())), new StreamResult(ba_stream));
// //Prepares the object to be returned
// StringBuffer buffer = new StringBuffer();
// try {
// InputStreamReader isr = new InputStreamReader( new ByteArrayInputStream(ba_stream.toByteArray()),
// "UTF8");
// Reader in2 = new BufferedReader(isr);
// int ch;
// while ((ch = in2.read()) > -1) {
// buffer.append((char)ch);
// }
// in2.close();
// return buffer.toString();
// } catch (Exception e) {
// logger.error("Exception:", e);
// }
// }
// catch (Exception e) {
// logger.error("Exception:", e);
// }
// return null;
// }
//}