Package org.exist.xmldb

Examples of org.exist.xmldb.XmldbURI


            broker = pool.get(user);

            if(xqueryresource.indexOf(':') > 0) {
                source = SourceFactory.getSource(broker, "", xqueryresource, true);
            } else {
                final XmldbURI pathUri = XmldbURI.create(xqueryresource);
                resource = broker.getXMLResource(pathUri, Lock.READ_LOCK);

                if(resource != null) {
                    source = new DBSource(broker, (BinaryDocument)resource, true);
                }
View Full Code Here


 
  private Source getQuerySource(DBBroker broker, String scriptURI, String script) {
    if(scriptURI != null) {
          DocumentImpl resource = null;
          try {
        final XmldbURI pathUri = XmldbURI.create(scriptURI);
       
        resource = broker.getXMLResource(pathUri, Lock.READ_LOCK);
        if (resource != null)
          {return new DBSource(broker, (BinaryDocument)resource, true);}
       
View Full Code Here

        if (args.length == 0){
            return true;
        }
       
        try {
            XmldbURI newPath = path;
            final XmldbURI currUri = XmldbURI.xmldbUriFor(properties.getProperty("uri")).resolveCollectionPath(path);
            if (args[0].equalsIgnoreCase("ls")) {
                // list collection contents
                getResources();
                if ("true".equals(properties.getProperty("permissions"))) {
                    for (int i = 0; i < resources.length; i++) {
                        messageln(resources[i]);
                    }
                } else {
                    for (int i = 0; i < resources.length; i++) {
                        final StringBuilder buf = new StringBuilder();
                        int k = 0;
                        for (int j = 0; i < resources.length && j < 5; i++, j++) {
                            buf.append(resources[i]);
                            buf.append('\t');
                            k = j;
                        }
                        if (k == 4 && i < resources.length) {
                            i--;
                        }
                        messageln(buf.toString());
                    }
                }
            } else if (args[0].equalsIgnoreCase("cd")) {
                // change current collection
                completitions.clear();
                Collection temp;
                XmldbURI collectionPath;
                if (args.length < 2 || args[1] == null) {
                    collectionPath = XmldbURI.ROOT_COLLECTION_URI;
                } else {
                    collectionPath = XmldbURI.xmldbUriFor(URIUtils.urlEncodeUtf8(args[1]));
                }
                collectionPath = currUri.resolveCollectionPath(collectionPath);
                if(collectionPath.numSegments()==0) {
                    collectionPath = currUri.resolveCollectionPath(XmldbURI.ROOT_COLLECTION_URI);
                    messageln("cannot go above "+XmldbURI.ROOT_COLLECTION_URI.toString());
                }
                temp = DatabaseManager.getCollection(
                    collectionPath.toString(),
                    properties.getProperty("user"),
                    properties.getProperty("password"));
                if (temp != null) {
                    current = temp;
                    newPath = collectionPath.toCollectionPathURI();
                    if (startGUI) {
                        frame.setPath(collectionPath.toCollectionPathURI());
                    }
                } else {
                    messageln("no such collection.");
                }
                getResources();
            } else if (args[0].equalsIgnoreCase("cp")) {
                if (args.length != 3) {
                    messageln("cp requires two arguments.");
                    return true;
                }
                final XmldbURI src,dest;
                try {
                    src = URIUtils.encodeXmldbUriFor(args[1]);
                    dest = URIUtils.encodeXmldbUriFor(args[2]);
                } catch(final URISyntaxException e) {
                    errorln("could not parse collection name into a valid URI: "+e.getMessage());
                    return false;
                }
                copy(src,dest);
                getResources();
               
            } else if (args[0].equalsIgnoreCase("edit")) {
                if (args.length == 2) {
                    final XmldbURI resource;
                    try {
                      resource = URIUtils.encodeXmldbUriFor(args[1]);
                    } catch(final URISyntaxException e) {
                        errorln("could not parse resource name into a valid URI: "+e.getMessage());
                        return false;
                    }
                    editResource(resource);
                } else {
                    messageln("Please specify a resource.");
                }
            } else if (args[0].equalsIgnoreCase("get")) {
                if (args.length < 2) {
                    System.err.println("wrong number of arguments.");
                    return true;
                }
                final XmldbURI resource;
                try {
                    resource = URIUtils.encodeXmldbUriFor(args[1]);
                } catch(final URISyntaxException e) {
                    errorln("could not parse resource name into a valid URI: "+e.getMessage());
                    return false;
                }
                final Resource res = retrieve(resource);
                // display document
                if (res != null) {
                    final String data;
                    if ("XMLResource".equals(res.getResourceType())) {
                        data = (String) res.getContent();
                    } else {
                        data = new String((byte[]) res.getContent());
                    }
                    if (startGUI) {
                        frame.setEditable(false);
                        frame.display(data);
                        frame.setEditable(true);
                    } else {
                        final String content = data;
                        more(content);
                    }
                }
                return true;
            } else if (args[0].equalsIgnoreCase("find")) {
                // search
                if (args.length < 2) {
                    messageln("no query argument found.");
                    return true;
                }
                messageln(args[1]);
                final long start = System.currentTimeMillis();
                result = find(args[1]);
                if (result == null) {
                    messageln("nothing found");
                } else {
                    messageln("found " + result.getSize() + " hits in "
                            + (System.currentTimeMillis() - start) + "ms.");
                }
               
                nextInSet = 1;
               
            } else if (args[0].equalsIgnoreCase("run")) {
                if (args.length < 2) {
                    messageln("please specify a query file.");
                    return true;
                }
                try {
                    final BufferedReader reader = new BufferedReader(new FileReader(args[1]));
                    final StringBuilder buf = new StringBuilder();
                    String nextLine;
                    while ((nextLine = reader.readLine()) != null) {
                        buf.append(nextLine);
                        buf.append(EOL);
                    }
                    args[1] = buf.toString();
                    final long start = System.currentTimeMillis();
                    result = find(args[1]);
                    if (result == null) {
                        messageln("nothing found");
                    } else {
                        messageln("found " + result.getSize() + " hits in "
                                + (System.currentTimeMillis() - start) + "ms.");
                    }
                   
                    nextInSet = 1;
                } catch (final Exception e) {
                    errorln("An error occurred: " + e.getMessage());
                }
            } else if (args[0].equalsIgnoreCase("show")) {
                // show search results
                if (result == null) {
                    messageln("no result set.");
                    return true;
                }
                try {
                    int start = nextInSet;
                    int count = 1;
                    if (args.length > 1) {
                        start = Integer.parseInt(args[1]);
                    }
                   
                    if (args.length > 2) {
                        count = Integer.parseInt(args[2]);
                    }
                   
                    final int s = (int) result.getSize();
                    if (start < 1 || start > s) {
                        messageln("start offset out of range");
                        return true;
                    }
                    --start;
                    if (start + count > s) {
                        count = s - start;
                    }
                   
                    nextInSet = start + count + 1;
                    for (int i = start; i < start + count; i++) {
                        final Resource r = result.getResource(i);
                        if (startGUI) {
                            frame.display((String) r.getContent());
                        } else {
                            more((String) r.getContent());
                        }
                    }
                    messageln("displayed items " + (start + 1) + " to "
                            + (start + count) + " of " + result.getSize());
                } catch (final NumberFormatException nfe) {
                    errorln("wrong argument");
                    return true;
                }
               
            } else if (args[0].equalsIgnoreCase("mkcol")) {
                // create collection
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
                final XmldbURI collUri;
                try {
                    collUri = URIUtils.encodeXmldbUriFor(args[1]);
                } catch(final URISyntaxException e) {
                    errorln("could not parse collection name into a valid URI: "+e.getMessage());
                    return false;
                }
                final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current
                        .getService("CollectionManagementService", "1.0");
                final Collection newCollection = mgtService.createCollection(collUri);
                if (newCollection == null) {
                    messageln("could not create collection.");
                } else {
                    messageln("created collection.");
                }
               
                // re-read current collection
                current = DatabaseManager.getCollection(properties
                        .getProperty("uri")
                        + path, properties.getProperty("user"), properties
                        .getProperty("password"));
                getResources();
               
            } else if (args[0].equalsIgnoreCase("put")) {
                // put a document or directory into the database
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
                final boolean r = parse(args[1]);
                getResources();
                return r;
               
            } else if (args[0].equalsIgnoreCase("putzip")) {
                // put the contents of a zip archive into the database
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
                final boolean r = parseZip(args[1]);
                getResources();
                return r;
               
            } else if (args[0].equalsIgnoreCase("putgz")) {
                // put the contents of a zip archive into the database
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
                final boolean r = parseGZip(args[1]);
                getResources();
                return r;
               
            } else if (args[0].equalsIgnoreCase("blob")) {
                // put a document or directory into the database
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
                storeBinary(args[1]);
                getResources();
               
            } else if (args[0].equalsIgnoreCase("rm")) {
                // remove document
                if (args.length < 2) {
                    messageln("missing argument.");
                    return true;
                }
               
                remove(args[1]);
               
                // re-read current collection
                current = DatabaseManager.getCollection(properties
                        .getProperty("uri")
                        + path, properties.getProperty("user"), properties
                        .getProperty("password"));
                getResources();
               
            } else if (args[0].equalsIgnoreCase("rmcol")) {
                // remove collection
                if (args.length < 2) {
                    messageln("wrong argument count.");
                    return true;
                }
                final XmldbURI collUri;
                try {
                    collUri = URIUtils.encodeXmldbUriFor(args[1]);
                } catch(final URISyntaxException e) {
                    errorln("could not parse collection name into a valid URI: "+e.getMessage());
                    return false;
View Full Code Here

        messageln("done.");
    }
   
    private void copy(final XmldbURI source, XmldbURI destination) throws XMLDBException {
        final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl)current.getService("CollectionManagementService", "1.0");
        final XmldbURI destName = destination.lastSegment();
        final Collection destCol = resolveCollection(destination);
        if(destCol == null) {
            if(destination.numSegments()==1) {
                destination = XmldbURI.create(current.getName());
            } else {
                destination = destination.removeLastSegment();
            }
        }
        final Resource srcDoc = resolveResource(source);
        if(srcDoc != null) {
            final XmldbURI resourcePath = XmldbURI.create(srcDoc.getParentCollection().getName()).append(srcDoc.getId());
            messageln("Copying resource '" + resourcePath + "' to '" + destination + "'");
            mgtService.copyResource(resourcePath, destination, destName);
        } else {
            messageln("Copying collection '" + source + "' to '" + destination + "'");
            mgtService.copy(source, destination, destName);
View Full Code Here

        final File files[] = dir.listFiles();
        Collection c;
        Resource document;
        CollectionManagementServiceImpl mgtService;
        //The XmldbURIs here aren't really used...
        XmldbURI next;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            next = base.append(files[i].getName());
            try {
                if (files[i].isDirectory()) {
View Full Code Here

        final File files[] = dir.listFiles();
        Collection c;
        Resource document;
        CollectionManagementServiceImpl mgtService;
        //The XmldbURIs here aren't really used...
        XmldbURI next;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            next = base.append(files[i].getName());
            try {
                if (files[i].isDirectory()) {
View Full Code Here

        if (!file.canRead()) {
            upload.showMessage(file.getAbsolutePath()+ " impossible to read ");
            return;
        }
       
        final XmldbURI filenameUri;
        try {
            filenameUri = URIUtils.encodeXmldbUriFor(file.getName());
        } catch (final URISyntaxException e1) {
            upload.showMessage(file.getAbsolutePath()+ " could not be encoded as a URI");
            return;
        }
       
        // Directory, create collection, and crawl it
        if (file.isDirectory()) {
            Collection c=null;
            try {
                c = collection.getChildCollection(filenameUri.toString());
                if(c == null) {
                    final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) collection.getService("CollectionManagementService","1.0");
                    c = mgtService.createCollection(filenameUri);
                }
            } catch (final XMLDBException e) {
                upload.showMessage("Impossible to create a collection " + file.getAbsolutePath() + ": " + e.getMessage());
            }
           
            // change displayed collection if it's OK
            upload.setCurrentDir(file.getAbsolutePath());
            if (c instanceof Observable) {
                ((Observable) c).addObserver(upload.getObserver());
            }
            // maybe a depth or recurs flag could be added here
            final File temp[] = file.listFiles();
            if (temp != null) {
                for (int i = 0; i < temp.length; i++) {
                    store(c, temp[i], upload);
                }
            }
            return;
        }
       
        // File, create and store resource
        if (file.isFile()) {
            upload.reset();
            upload.setCurrent(file.getName());
            upload.setCurrentSize(file.length());
           
            MimeType mimeType = MimeTable.getInstance().getContentTypeFor(file.getName());
            // unknown mime type, here prefered is to do nothing
            if(mimeType == null) {
                upload.showMessage(file.getAbsolutePath() +
                    " - unknown suffix. No matching mime-type found in : " +
                    MimeTable.getInstance().getSrc());
               
                // if some one prefers to store it as binary by default, but dangerous
                mimeType = MimeType.BINARY_TYPE;
            }
           
            try {
                final Resource res = collection.createResource(filenameUri.toString(), mimeType.getXMLDBType());
                ((EXistResource) res).setMimeType(mimeType.getName());
                res.setContent(file);
                collection.storeResource(res);
                ++filesCount;
                this.totalLength += file.length();
View Full Code Here

   
   
    private void mkcol(final XmldbURI collPath) throws XMLDBException {
        messageln("creating '" + collPath + "'");
        final XmldbURI[] segments = collPath.getPathSegments();
        XmldbURI p = XmldbURI.ROOT_COLLECTION_URI;
        for(int i=1;i<segments.length;i++) {
            p = p.append(segments[i]);
            final Collection c = DatabaseManager.getCollection(properties.getProperty("uri") + p, properties.getProperty("user"), properties.getProperty("password"));
            if (c == null) {
                final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) current.getService("CollectionManagementService", "1.0");
                current = mgtService.createCollection(segments[i]);
            } else {
View Full Code Here

            properties.getProperty("user"),
            properties.getProperty("password"));
    }
   
    private Resource resolveResource(final XmldbURI path) throws XMLDBException {
      final XmldbURI collectionPath = path.numSegments()==1?XmldbURI.create(current.getName()):path.removeLastSegment();
      final XmldbURI resourceName = path.lastSegment();
       
        final Collection collection = resolveCollection(collectionPath);
        if(collection == null) {
            messageln("Collection " + collectionPath + " not found.");
            return null;
        }
        messageln("Locating resource " + resourceName + " in collection " + collection.getName());
        return collection.getResource(resourceName.toString());
    }
View Full Code Here

                    // try for a doc
                    DocumentImpl doc = null;
                    try
                    {
                        XmldbURI xmldburi = XmldbURI.create(uri);
                        doc = context.getBroker().getXMLResource(xmldburi, Lock.READ_LOCK);

                        if(doc == null)
                        {
                            // no doc, try for a collection
View Full Code Here

TOP

Related Classes of org.exist.xmldb.XmldbURI

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.