Revision: 20016 http://sourceforge.net/p/gate/code/20016 Author: markagreenwood Date: 2017-01-30 16:05:37 +0000 (Mon, 30 Jan 2017) Log Message: ----------- loading of persisted ResourceReference instances now works (or at least my test passes) with proper handling of $ etc. This is done by gerneralizing the way URLs are handled so the same code now works with URLs and URIs. So if something stops working with a URL this is probably where I should look for the bug
Modified Paths: -------------- gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java gate/branches/sawdust2/gate-core/src/main/java/gate/util/persistence/PersistenceManager.java gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java 2017-01-30 11:33:40 UTC (rev 20015) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java 2017-01-30 16:05:37 UTC (rev 20016) @@ -128,10 +128,6 @@ public URL toURL() throws IOException { - if(!uri.isAbsolute()) - throw new IOException( - "Unable to access relative resource reference: " + uri); - if(!uri.getScheme().equals("creole")) return uri.toURL(); try { Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/util/persistence/PersistenceManager.java =================================================================== --- gate/branches/sawdust2/gate-core/src/main/java/gate/util/persistence/PersistenceManager.java 2017-01-30 11:33:40 UTC (rev 20015) +++ gate/branches/sawdust2/gate-core/src/main/java/gate/util/persistence/PersistenceManager.java 2017-01-30 16:05:37 UTC (rev 20016) @@ -162,7 +162,9 @@ } public static class RRPersistence implements Persistence { - + + private static final long serialVersionUID = 6543151074741224114L; + String uriString; @Override @@ -214,10 +216,10 @@ @Override public Object createObject() throws PersistenceException, ResourceInstantiationException { - // TODO Auto-generated method stub - return null; - } - + + return URLHolder.unpackPersistentRepresentation(uriString); + + } } /** @@ -452,13 +454,20 @@ * supposed to be a copy for the original object used as source for * data extraction. */ - // TODO: this always uses the one-argument constructor for URL. This may not always - // do the right thing though, we should test this (e.g. when the URL contains %-encoded parts) @Override public Object createObject() throws PersistenceException { try { - if(urlString.startsWith(relativePathMarker)) { - URL context = currentPersistenceURL(); + return unpackPersistentRepresentation(urlString).toURL(); + } catch(MalformedURLException e) { + throw new PersistenceException(e); + } + } + + public static URI unpackPersistentRepresentation(String persistent) throws PersistenceException { + try { + + if(persistent.startsWith(relativePathMarker)) { + URI context = currentPersistenceURL().toURI(); // If the part after the $relpath$ marker is empty, the normal method // would get us the URL of the context which will be the URL of the pipeline, not // of the directory where the pipeline is located. @@ -471,37 +480,37 @@ // If the relative part is starting with a slash, we remove that //URL ret = new URL(context, urlString.substring(relativePathMarker // .length())); - URL ret = combineContextAndRelative(context, urlString.substring(relativePathMarker + URI ret = combineContextAndRelative(context, persistent.substring(relativePathMarker .length()), true); logger.debug("CurrentPresistenceURL is "+context+" created="+ret); return ret; - } else if(urlString.startsWith(gatehomePathMarker)) { + } else if(persistent.startsWith(gatehomePathMarker)) { URL gatehome = getCanonicalFileIfPossible(getGateHomePath()).toURI().toURL(); //return new URL(gatehome, urlString.substring(gatehomePathMarker.length())); - return combineContextAndRelative(gatehome, urlString.substring(gatehomePathMarker.length()),false); - } else if(urlString.startsWith(gatepluginsPathMarker)) { + return combineContextAndRelative(gatehome.toURI(), persistent.substring(gatehomePathMarker.length()),false); + } else if(persistent.startsWith(gatepluginsPathMarker)) { URL gateplugins = Gate.getPluginsHome().toURI().toURL(); //return new URL(gateplugins, urlString.substring(gatepluginsPathMarker.length())); - return combineContextAndRelative(gateplugins, urlString.substring(gatepluginsPathMarker.length()),false); - } else if(urlString.startsWith(resourceshomePathMarker)) { + return combineContextAndRelative(gateplugins.toURI(), persistent.substring(gatepluginsPathMarker.length()),false); + } else if(persistent.startsWith(resourceshomePathMarker)) { if(getResourceshomePath() == null) { - throw new GateRuntimeException("Cannot restore URL "+urlString+ + throw new GateRuntimeException("Cannot restore URL "+persistent+ "property "+resourceshomePropertyName+" is not set"); } URL resourceshomeurl = getResourceshomePath().toURI().toURL(); //return new URL(resourceshomeurl, urlString.substring(resourceshomePathMarker.length())); - return combineContextAndRelative(resourceshomeurl, urlString.substring(resourceshomePathMarker.length()),false); - } else if(urlString.startsWith(syspropMarker)) { - String urlRestString = urlString.substring(syspropMarker.length()); + return combineContextAndRelative(resourceshomeurl.toURI(), persistent.substring(resourceshomePathMarker.length()),false); + } else if(persistent.startsWith(syspropMarker)) { + String urlRestString = persistent.substring(syspropMarker.length()); int dollarindex = urlRestString.indexOf("$"); if(dollarindex > 0) { String syspropname = urlRestString.substring(0,dollarindex); String propvalue = System.getProperty(syspropname); if(propvalue == null) { - throw new PersistenceException("Property '"+syspropname+"' is null in "+urlString); + throw new PersistenceException("Property '"+syspropname+"' is null in "+persistent); } // TODO: we should only assume a file if what we get does not start with a protocol - URL propuri = (new File(propvalue)).toURI().toURL(); + URI propuri = (new File(propvalue)).toURI(); if(dollarindex == urlRestString.length()) { return propuri; } else { @@ -509,15 +518,15 @@ return combineContextAndRelative(propuri, urlRestString.substring(dollarindex+1),false); } } else if(dollarindex == 0) { - throw new PersistenceException("No property name after '"+syspropMarker+"' in "+urlString); + throw new PersistenceException("No property name after '"+syspropMarker+"' in "+persistent); } else { - throw new PersistenceException("No ending $ after '"+syspropMarker+"' in "+urlString); + throw new PersistenceException("No ending $ after '"+syspropMarker+"' in "+persistent); } } else { - return new URL(urlString); + return new URI(persistent); } } - catch(MalformedURLException mue) { + catch(MalformedURLException | URISyntaxException mue) { throw new PersistenceException(mue); } } @@ -526,7 +535,7 @@ //******** Helper methods just used inside the URLHolder class - private URL combineContextAndRelative(URL context, String relative, boolean contextIsFile) { + private static URI combineContextAndRelative(URI context, String relative, boolean contextIsFile) { // make sure we always have a proper string if(relative==null) relative = ""; // we always want to interpret the part after the marker as a relative path, so @@ -538,7 +547,7 @@ if(relative.isEmpty()) { if(!contextIsFile) return context; // context is file, get the parent and return that - URI tmpUri; + /*URI tmpUri; try { tmpUri = context.toURI(); } catch (URISyntaxException ex) { @@ -550,17 +559,18 @@ context = tmpUri.toURL(); } catch (Exception ex) { throw new GateRuntimeException("Could not convert context URI to URL: "+tmpUri,ex); - } - return context; + }*/ + return context.resolve("."); } else { // use the URL constructor to splice the two parts together - URL tmpUrl; + /*URL tmpUrl; try { tmpUrl = new URL(context,relative); } catch (Exception ex) { throw new GateRuntimeException("Could not create a URL from context "+context+" and relative part "+relative,ex); } - return tmpUrl; + return tmpUrl;*/ + return context.resolve(relative); } } @@ -1274,8 +1284,8 @@ try { if (anUrl instanceof URL) Gate.getCreoleRegister().registerPlugin(new Plugin.Directory((URL)anUrl),false); - else - Gate.getCreoleRegister().registerPlugin((Plugin)anUrl, false); + else if (anUrl instanceof Plugin) + Gate.getCreoleRegister().registerPlugin((Plugin)anUrl, false); } catch(GateException ge) { System.out.println("We've hit an error!"); Modified: gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java =================================================================== --- gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java 2017-01-30 11:33:40 UTC (rev 20015) +++ gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java 2017-01-30 16:05:37 UTC (rev 20016) @@ -6,7 +6,6 @@ import java.net.URL; import org.apache.commons.io.IOUtils; -import org.jdom.Content; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; @@ -39,40 +38,46 @@ @CreoleParameter(defaultValue = "resources/file.txt") public void setParam(ResourceReference rr) { this.rr = rr; + } + } + + public static class TestPlugin extends Plugin.Maven { + + public TestPlugin() { + super("group", "artifact", "version"); } - } + @Override + public URL getBaseURL() { + try { + return new URL(TestDocument.getTestServerName() + "tests/"); + } catch(Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public Document getCreoleXML() throws Exception, JDOMException { + Document doc = new Document(); + Element element = null; + doc.addContent(element = new Element("CREOLE-DIRECTORY")); + + element.addContent(element = new Element("CREOLE")); + element.addContent(element = new Element("RESOURCE")); + Element classElement = new Element("CLASS"); + classElement.setText(TestResource.class.getName()); + element.addContent(classElement); + return doc; + } + }; + @Override public void setUp() throws Exception { // Initialise the GATE library and creole register Gate.init(); + + creolePlugin = new TestPlugin(); - creolePlugin = new Plugin.Maven("group", "artifact", "version") { - - @Override - public URL getBaseURL() { - try { - return new URL(TestDocument.getTestServerName() + "tests/"); - } catch(Exception e) { - throw new RuntimeException(e); - } - } - - @Override - public Document getCreoleXML() throws Exception, JDOMException { - Document doc = new Document(); - Element element = null; - doc.addContent(element = new Element("CREOLE-DIRECTORY")); - - element.addContent(element = new Element("CREOLE")); - element.addContent(element = new Element("RESOURCE")); - Element classElement = new Element("CLASS"); - classElement.setText(TestResource.class.getName()); - element.addContent(classElement); - return doc; - } - }; - Gate.getCreoleRegister().registerPlugin(creolePlugin); } @@ -196,19 +201,20 @@ public void testPersistence() throws Exception { - Resource resource = null; + Resource resource = null, restored = null; File xgappFile = null; File txtFile = null; try { xgappFile = File.createTempFile("rr-test", ".xgapp"); - xgappFile.createNewFile(); - + txtFile = new File(xgappFile.getParentFile(),"test-file.txt"); + ResourceReference rr1 = new ResourceReference(txtFile.toURI()); + FeatureMap params = Factory.newFeatureMap(); - params.put("param", new ResourceReference(txtFile.toURI())); + params.put("param", rr1); resource = Factory.createResource(TestResource.class.getName(),params); PersistenceManager.saveObjectToFile(resource, xgappFile); @@ -218,7 +224,7 @@ Element entry = doc.getRootElement().getChild("application").getChild("initParams").getChild("localMap").getChild("entry"); - assertEquals("couldn't find a param entry", "param",entry.getChildText("string")); + assertEquals("couldn't find the paramameter entry", "param",entry.getChildText("string")); Element value = entry.getChild("gate.util.persistence.PersistenceManager-RRPersistence"); @@ -226,6 +232,13 @@ assertEquals("The URI was not as expected","$relpath$test-file.txt", value.getChildText("uriString")); + //System.out.println(IOUtils.toString(xgappFile.toURI().toURL().openStream())); + restored = (Resource)PersistenceManager.loadObjectFromFile(xgappFile); + + ResourceReference rr2 = (ResourceReference)restored.getParameterValue("param"); + + assertEquals(rr1, rr2); + } finally { if (xgappFile != null) xgappFile.deleteOnExit(); @@ -233,7 +246,9 @@ Factory.deleteResource(resource); } - + if(restored != null) { + Factory.deleteResource(restored); + } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot _______________________________________________ GATE-cvs mailing list GATE-cvs@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gate-cvs