private String subjectAsURI(String subj) { // already a uri? if (subj.startsWith(Constants.FEDORA.uri)) { return subj; } // no, convert to uri return PID.toURI(subj); }
/** * hashCode() should return the same value for lexically equivalent PIDs. */ @Test public void testHashCodeSamePID() throws Exception { PID pid1 = PID.getInstance("test:somepid"); PID pid2 = new PID("test:somepid"); PID pid3 = PID.fromFilename("test_somepid"); assertEquals(pid1.hashCode(), pid2.hashCode()); assertEquals(pid2.hashCode(), pid3.hashCode()); }
PID p = new PID(args[0]); System.out.println("Normalized : " + p.toString()); System.out.println("To filename : " + p.toFilename()); System.out.println("From filename : " + PID.fromFilename(p.toFilename()).toString()); } else { System.out.println("--------------------------------------"); done = true; } else { PID p = new PID(line); System.out.println("Normalized : " + p.toString()); System.out.println("To filename : " + p.toFilename()); System.out.println("From filename : " + PID.fromFilename(p.toFilename()).toString());
protected String getSubjectURI(String subject) throws MalformedPIDException { String strippedSubject; if (subject.startsWith(Constants.FEDORA.uri)) { strippedSubject = subject.substring(Constants.FEDORA.uri.length()); } else { strippedSubject = subject; } String parts[] = strippedSubject.split("/"); PID pid = new PID(parts[0]); String subjectURI; if (parts.length == 1) { subjectURI = pid.toURI(); } else if (parts.length == 2) { subjectURI = pid.toURI() + "/" + parts[1]; // add datastream } else { logger.warn("Invalid subject argument for getRelationships: " + subject + ". Should be pid or datastream (URI form optional"); subjectURI = null; } return subjectURI; }
/** * Convert a policy name to a filename that can be used to persist the policy. * Policy names must be valid PIDs (the DO managing the policy) * @param policyName * @return * @throws PolicyIndexException */ private File nameToFile(String policyName) throws PolicyIndexException { PID pid; try { pid = new PID(policyName); } catch (MalformedPIDException e) { throw new PolicyIndexException("Invalid policy name. Policy name must be a valid PID - " + policyName); } return new File(DB_HOME + "/" + pid.toFilename() + ".xml"); } /**
private Feed createAtomObject(String spid, String contentLocation) throws Exception { PID pid = PID.getInstance(spid); Date date = new Date(1); String title = "title"; String author = "org.fcrepo.test.api.TestManagedDatastreams"; Feed feed = abdera.newFeed(); feed.setId(pid.toURI()); feed.setTitle(title); feed.setUpdated(date); feed.addAuthor(author); if (contentLocation != null && contentLocation.length() > 0) { addAtomManagedDatastream(feed, contentLocation); } return feed; }
private Foxml11Document createFoxmlObject(String spid, String contentLocation) throws Exception { PID pid = PID.getInstance(spid); Date date = new Date(1); Foxml11Document doc = new Foxml11Document(pid.toString()); doc.addObjectProperty(Property.STATE, "A"); if (contentLocation != null && contentLocation.length() > 0) { String ds = "DS"; String dsv = "DS1.0"; doc.addDatastream(ds, State.A, ControlGroup.E, true); doc.addDatastreamVersion(ds, dsv, "text/plain", "label", 1, date); doc.setContentLocation(dsv, contentLocation, org.fcrepo.server.storage.types.Datastream.DS_LOCATION_TYPE_URL); } return doc; }
/** * {@inheritDoc} */ @Override public boolean equals(Object o) { return o instanceof PID && m_normalized.equals(((PID) o).toString()); }
/** * equals() should return false for lexically distinct PIDs. */ @Test public void testEqualsDifferentPID() throws Exception { PID pid1 = PID.getInstance("test:somepid"); PID pid2 = new PID("test:someotherpid"); PID pid3 = PID.fromFilename("test_yetanotherpid"); assertFalse(pid1.equals(pid2)); assertFalse(pid2.equals(pid1)); assertFalse(pid2.equals(pid3)); assertFalse(pid3.equals(pid2)); }
/** * Returns a PID object for the requested String. This method will return a * PID for a variety of pid permutations, e.g. demo:1, info:fedora/demo:1, * demo:1/DS1, info:fedora/demo:1/sdef:foo/sdep:bar/methodBaz. * * @param pid * @return a PID object */ // FIXME: does not appear to be used anywhere protected PID getNormalizedPID(String pid) { // strip the leading "info:fedora/" if any if (pid.startsWith(Constants.FEDORA.uri)) { pid = pid.substring(Constants.FEDORA.uri.length()); } // should be left with "demo:foo" or "demo:foo/demo:bar" return PID.getInstance(pid.split("\\/")[0]); }
/** * equals() should return true for lexically equivalent PIDs. */ @Test public void testEqualsSamePID() throws Exception { PID pid1 = PID.getInstance("test:somepid"); PID pid2 = new PID("test:somepid"); PID pid3 = PID.fromFilename("test_somepid"); assertEquals(pid1, pid2); assertEquals(pid2, pid3); }
DOManager doManager, RDFName objectName) throws Exception { PID pid = new PID(objectName.uri.substring("info:fedora/".length())); boolean exists = doManager.objectExists(pid.toString()); if (exists && firstRun) { logger.info("Purging old system object: {}", pid); DOWriter w = doManager.getWriter(USE_DEFINITIVE_STORE, context, pid.toString()); w.remove(); try { logger.info("Ingesting new system object: {}", pid); InputStream xml = getStream("org/fcrepo/server/resources/" + pid.toFilename() + ".xml"); Context context = ReadOnlyContext.getContext(null, null,
/** * Construct a PID given a filename of the form produced by toFilename(), * throwing a MalformedPIDException if it's not well-formed. * @param filenameString default translation of PID to filename * @return PID the PID producing the input filename * @throws MalformedPIDException */ public static PID fromFilename(String filenameString) throws MalformedPIDException { String decoded = filenameString.replaceFirst("_", ":"); if (decoded.endsWith("%")) { decoded = decoded.substring(0, decoded.length() - 1) + "."; } return new PID(decoded); }
/** * Determine name of policy from file. .xml prefix is removed, and name is converted to a PID. * Policy names must be valid PIDs. * @param policyFile * @return * @throws PolicyIndexException */ private String fileToName(File policyFile) throws PolicyIndexException { try { if (!policyFile.getName().endsWith(".xml")) throw new PolicyIndexException("Invalid policy file name. Policy files must end in .xml - " + policyFile.getName()); return PID.fromFilename(policyFile.getName().substring(0, policyFile.getName().lastIndexOf(".xml"))).toString(); } catch (MalformedPIDException e) { throw new PolicyIndexException("Invalid policy file name. Filename cannot be converted to a valid PID - " + policyFile.getName()); } }
/** * Cause the given PID to never be generated by the PID generator. */ public synchronized void neverGeneratePID(String pid) throws IOException { logger.debug("Never generating PID: {}", pid); try { PID p = new PID(pid); String ns = p.getNamespaceId(); int id = Integer.parseInt(p.getObjectId()); if (id > getHighestID(ns)) { setHighestID(ns, id); } } catch (MalformedPIDException mpe) { throw new IOException(mpe.getMessage()); } catch (NumberFormatException nfe) { // if the id part is not numeric, we already know we'll // never generate that id because all generated ids are numeric. } }
public static PID pidFromFilename(String filename) throws MalformedPidException { try { return PID.fromFilename(filename); } catch (MalformedPIDException e) { throw new MalformedPidException(e.getMessage()); } }
/** * Construct a PID from a string, throwing a MalformedPIDException if it's * not well-formed. */ public PID(String pidString) throws MalformedPIDException { if (pidString.startsWith(Constants.FEDORA.uri)) { m_normalized = normalize(pidString, Constants.FEDORA.uri.length(), pidString.length()); } else { m_normalized = normalize(pidString); } int colon = m_normalized.indexOf(':'); m_namespaceId = m_normalized.substring(0, colon); m_objectId = m_normalized.substring(colon + 1); }
public static String encode(String unencoded) throws LowlevelStorageException { try { int i = unencoded.indexOf("+"); if (i != -1) { return Server.getPID(unencoded.substring(0, i)).toFilename() + unencoded.substring(i); } else { return Server.getPID(unencoded).toFilename(); } } catch (MalformedPidException e) { throw new LowlevelStorageException(true, e.getMessage(), e); } }
/** * Get the RELS-EXT xml for an object. */ public static String getRELSEXT(String pid, String content) { StringBuilder x = new StringBuilder(185 + content.length() + pid.length()); x.append("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"" + " xmlns:foo=\"http://example.org/foo#\">\n" + "<rdf:Description rdf:about=\""); x.append(PID.getInstance(pid).toURI()); x.append("\">\n"); x.append(content); x.append("\n</rdf:Description>\n</rdf:RDF>"); return x.toString(); }