Package org.xmldb.api.base

Examples of org.xmldb.api.base.Resource


     */
    protected synchronized boolean parse(String fileName) throws XMLDBException {
        //TODO : why is this test for ? File should make it, shouldn't it ? -pb
        fileName = fileName.replace('/', File.separatorChar).replace('\\',File.separatorChar);
        final File file = new File(fileName);
        Resource document;
        // String xml;
       
        if (current instanceof Observable && verbose) {
            final ProgressObserver observer = new ProgressObserver();
            ((Observable) current).addObserver(observer);
        }
       
        final File files[];
        if (file.canRead()) {
            // TODO, same logic as for the graphic client
            if (file.isDirectory()) {
                if (recurseDirs) {
                    filesCount = 0;
                    final long start = System.currentTimeMillis();
                    final boolean result = findRecursive(current, file, path);
                    messageln("storing " + filesCount + " files took " + ((System.currentTimeMillis() - start) / 1000) + "sec.");
                    return result;
                }
                files = file.listFiles();
            } else {
                files = new File[1];
                files[0] = file;
            }
        } else {
            files = DirectoryScanner.scanDir(fileName);
        }
       
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                continue;
            }
            final long start = System.currentTimeMillis();
            mimeType = MimeTable.getInstance().getContentTypeFor(files[i].getName());
            if(mimeType == null) {
                mimeType = MimeType.BINARY_TYPE;
            }
            document = current.createResource(files[i].getName(), mimeType.getXMLDBType());
            message("storing document " + files[i].getName() + " (" + (i + 1) + " of " + files.length + ") ...");
            document.setContent(files[i]);
            ((EXistResource)document).setMimeType(mimeType.getName());
            current.storeResource(document);
            messageln("done.");
            messageln("parsing " + files[i].length() + " bytes took " + (System.currentTimeMillis() - start) + "ms." + EOL);
            bytes += files[i].length();
View Full Code Here


   
   
    private synchronized boolean findGZipRecursive(final Collection collection, final File dir, final XmldbURI base) throws XMLDBException {
        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()) {
                    messageln("entering directory " + files[i].getAbsolutePath());
                    c = collection.getChildCollection(files[i].getName());
                    if (c == null) {
                        mgtService = (CollectionManagementServiceImpl) collection.getService("CollectionManagementService", "1.0");
                        c = mgtService.createCollection(URIUtils.encodeXmldbUriFor(files[i].getName()));
                    }
                    if (c instanceof Observable && verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) c).addObserver(observer);
                    }
                    findGZipRecursive(c, files[i], next);
                } else {
                    final long start1 = System.currentTimeMillis();
                    final String compressedName = files[i].getName();
                    String localName = compressedName;
                    final String[] cSuffix = {".gz",".Z"};
                    boolean isCompressed = false;
                    for(int isuf = 0; isuf < cSuffix.length; isuf++) {
                      final String suf = cSuffix[isuf];
                      if(localName.endsWith(suf)) {
                        // Removing compressed prefix to validate
                        localName=compressedName.substring(0, localName.length()-suf.length());
                        isCompressed=true;
                        break;
                      }
                    }
                    mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                    if(mimeType == null) {
                        messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
                      mimeType = MimeType.BINARY_TYPE;
        }
                    message("storing document " + compressedName + " (" + i + " of " + files.length + ") " + "...");
                    document = collection.createResource(URIUtils.urlEncodeUtf8(compressedName), mimeType.getXMLDBType());
                    document.setContent(isCompressed?new GZIPInputSource(files[i]):files[i]);
                    ((EXistResource)document).setMimeType(mimeType.getName());
                    collection.storeResource(document);
                    ++filesCount;
                    messageln(" " + files[i].length() + (isCompressed?" compressed":"") + " bytes in "
                          + (System.currentTimeMillis() - start1) + "ms.");
View Full Code Here

    protected synchronized boolean parseGZip(String fileName) throws XMLDBException {
        //TODO : why is this test for ? Fileshould make it, shouldn't it ? -pb
        fileName = fileName.replace('/', File.separatorChar).replace('\\',
                File.separatorChar);
        final File file = new File(fileName);
        Resource document;
        // String xml;
        if (current instanceof Observable && verbose) {
            final ProgressObserver observer = new ProgressObserver();
            ((Observable) current).addObserver(observer);
        }
        final File files[];
        if (file.canRead()) {
            // TODO, same logic as for the graphic client
            if (file.isDirectory()) {
                if (recurseDirs) {
                    filesCount = 0;
                    final long start = System.currentTimeMillis();
                    final boolean result = findGZipRecursive(current, file, path);
                    messageln("storing " + filesCount + " compressed files took "
                            + ((System.currentTimeMillis() - start) / 1000)
                            + "sec.");
                    return result;
                }
                files = file.listFiles();
            } else {
                files = new File[1];
                files[0] = file;
            }
        } else {
            files = DirectoryScanner.scanDir(fileName);
        }
       
        final long start0 = System.currentTimeMillis();
        long bytes = 0;
        MimeType mimeType;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                continue;
            }
            final long start = System.currentTimeMillis();
            final String compressedName = files[i].getName();
            String localName = compressedName;
            final String[] cSuffix = {".gz",".Z"};
            boolean isCompressed = false;
            for(int isuf = 0; isuf < cSuffix.length; isuf++) {
              final String suf = cSuffix[isuf];
              if(localName.endsWith(suf)) {
                    // Removing compressed prefix to validate
                    localName = compressedName.substring(0, localName.length()-suf.length());
                    isCompressed = true;
                    break;
              }
            }
            mimeType = MimeTable.getInstance().getContentTypeFor(localName);
            if(mimeType == null) {
                mimeType = MimeType.BINARY_TYPE;
            }
            document = current.createResource(compressedName,mimeType.getXMLDBType());
            message("storing document " + compressedName + " (" + (i + 1)
            + " of " + files.length + ") ...");
            document.setContent(isCompressed?new GZIPInputSource(files[i]):files[i]);
            ((EXistResource)document).setMimeType(mimeType.getName());
            current.storeResource(document);
            messageln("done.");
            messageln("parsing " + files[i].length() + (isCompressed?" compressed":"") + " bytes took "
                    + (System.currentTimeMillis() - start) + "ms." + EOL);
View Full Code Here

    protected synchronized boolean parseZip(String fileName) throws XMLDBException {
        fileName = fileName.replace('/', File.separatorChar).replace('\\', File.separatorChar);
       
        try {
            final ZipFile zfile = new ZipFile(fileName);
            Resource document;
            // String xml;
            if (current instanceof Observable && verbose) {
                final ProgressObserver observer = new ProgressObserver();
                ((Observable) current).addObserver(observer);
            }

            final long start0 = System.currentTimeMillis();
            long bytes = 0;
            MimeType mimeType;
            final Enumeration<? extends ZipEntry> e = zfile.entries();
            int number = 0;
       
            Collection base = current;
            String baseStr="";
            while(e.hasMoreElements()) {
                number++;
                final ZipEntry ze=e.nextElement();
                final String zeName = ze.getName().replace('\\','/');
                final String[] pathSteps = zeName.split("/");
                String currStr = pathSteps[0];
                for(int i=1;i<pathSteps.length-1;i++) {
                    currStr += "/"+pathSteps[i];
                }
                if(!baseStr.equals(currStr)) {
                    base=current;
                    for(int i=0;i<pathSteps.length-1;i++) {
                        Collection c = base.getChildCollection(pathSteps[i]);
                        if (c == null) {
                            final CollectionManagementServiceImpl mgtService = (CollectionManagementServiceImpl) base.getService("CollectionManagementService","1.0");
                            c = mgtService.createCollection(URIUtils.encodeXmldbUriFor(pathSteps[i]));
                        }
                        base=c;
                    }
                    if (base instanceof Observable && verbose) {
                        final ProgressObserver observer = new ProgressObserver();
                        ((Observable) base).addObserver(observer);
                    }
                    baseStr=currStr;
                    messageln("entering directory " + baseStr);
                }
                if (!ze.isDirectory()) {
                    final String localName=pathSteps[pathSteps.length-1];
                    final long start = System.currentTimeMillis();
                    mimeType = MimeTable.getInstance().getContentTypeFor(localName);
                    if(mimeType == null) {
                        mimeType = MimeType.BINARY_TYPE;
                    }
                    document = base.createResource(localName,mimeType.getXMLDBType());
                    message("storing Zip-entry document " + localName + " (" + (number)
                                    + " of " + zfile.size() + ") ...");
                    document.setContent(new ZipEntryInputSource(zfile,ze));
                    ((EXistResource)document).setMimeType(mimeType.getName());
                    base.storeResource(document);
                    messageln("done.");
                    messageln("parsing " + ze.getSize() + " bytes took "
                                    + (System.currentTimeMillis() - start) + "ms." + EOL);
View Full Code Here

                // 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();
                upload.setStoredSize(this.totalLength);
            } catch (final XMLDBException e) {
View Full Code Here

            }
        }
       
        if (cOpt.optionGet != null) {
            try {
                final Resource res = retrieve(cOpt.optionGet);
                if (res != null) {
                    // String data;
                    if ("XMLResource".equals(res.getResourceType())) {
                        if (cOpt.optionOutputFile != null) {
                            writeOutputFile(cOpt.optionOutputFile, res.getContent());
                        } else {
                            System.out.println(res.getContent().toString());
                        }
                    } else {
                        if (cOpt.optionOutputFile != null) {
                            ((ExtendedResource)res).getContentIntoAFile(new File(cOpt.optionOutputFile));
                            ((EXistResource)res).freeResources();
                        } else {
                            ((ExtendedResource)res).getContentIntoAStream(System.out);
                            System.out.println();
                        }
                    }
                }
            } catch (final XMLDBException e) {
                System.err.println("XMLDBException while trying to retrieve document: " + getExceptionMessage(e));
                e.printStackTrace();
                return false;
            }
        } else if (cOpt.optionRemove != null) {
            if (!cOpt.foundCollection) {
                System.err.println("Please specify target collection with --collection");
            } else {
                try {
                    remove(cOpt.optionRemove);
                } catch (final XMLDBException e) {
                    System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
                    e.printStackTrace();
                    return false;
                }
            }
        } else if (cOpt.doStore) {
            if (!cOpt.foundCollection) {
                System.err.println("Please specify target collection with --collection");
            } else {
                for (final String arg : cOpt.optionalArgs) {
                    try {
                        parse(arg);
                    } catch (final XMLDBException e) {
                        System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
                        e.printStackTrace();
                        return false;
                    }
                }
            }
        } else if (cOpt.optionXpath != null || cOpt.optionQueryFile != null) {
            if (cOpt.optionQueryFile != null) {
                final BufferedReader reader = new BufferedReader(new FileReader(cOpt.optionQueryFile));
                try {
                    final StringBuilder buf = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                            buf.append(line);
                            buf.append(EOL);
                    }
                    cOpt.optionXpath = buf.toString();
                } finally {
                    reader.close();
                }
            }
            // if no argument has been found, read query from stdin
            if ("stdin".equals(cOpt.optionXpath)) {
                try {
                    final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                    final StringBuilder buf = new StringBuilder();
                    String line;
                    while ((line = stdin.readLine()) != null) {
                        buf.append(line);
                        buf.append(EOL);
                    }
                    cOpt.optionXpath = buf.toString();
                } catch (final IOException e) {
                    System.err.println("failed to read query from stdin");
                    cOpt.optionXpath = null;
                    return false;
                }
            }
            if (cOpt.optionXpath != null) {
                try {
                    final ResourceSet result = find(cOpt.optionXpath);
                    if (maxResults <= 0) {
                        maxResults = (int) result.getSize();
                    }
                    if (cOpt.optionOutputFile == null) {
                        for (int i = 0; i < maxResults && i < result.getSize(); i++) {
                            final Resource res=result.getResource(i);
                            if(res instanceof ExtendedResource) {
                                ((ExtendedResource)res).getContentIntoAStream(System.out);
                            } else {
                                System.out.println(res.getContent());
                            }
                        }
                    } else {
                      final FileOutputStream fos = new FileOutputStream(cOpt.optionOutputFile);
                      final BufferedOutputStream bos = new BufferedOutputStream(fos);
                      final PrintStream ps = new PrintStream(bos);
                        for (int i = 0; i < maxResults && i < result.getSize(); i++) {
                            final Resource res = result.getResource(i);
                            if(res instanceof ExtendedResource) {
                                ((ExtendedResource)res).getContentIntoAStream(ps);
                            } else {
                                ps.print(res.getContent().toString());
                            }
                        }
                        ps.close();
                        bos.close();
                        fos.close();
View Full Code Here

            for(final ResourceDescriptor desc : applyTo) {
                if (desc.isCollection()) {
                    final Collection coll = parent.getChildCollection(desc.getName().toString());
                    getUserManagementService().setPermissions(coll, lblOwnerValue.getText(), lblGroupValue.getText(), getBasicPermissionsTableModel().getMode(), dlgAces);
                } else {
                    final Resource res = parent.getResource(desc.getName().toString());
                    getUserManagementService().setPermissions(res, lblOwnerValue.getText(), lblGroupValue.getText(), getBasicPermissionsTableModel().getMode(), dlgAces);
                }
            }

            setVisible(false);
View Full Code Here

      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      node.create().encode(out);
      try
      {
        XMLResource xres;
        final Resource res = (documentName == null) ? null : policyCollection.getResource(documentName);
        if(res == null)
          {xres = null;}
        else if(res instanceof XMLResource)
          {xres = (XMLResource)res;}
        else
        {
          xres = null;
          policyCollection.removeResource(res);
        }
       
        if(xres == null)
        {
          xres = (XMLResource)policyCollection.createResource(documentName, "XMLResource");
          if(documentName == null)
          {
            documentName = xres.getDocumentId();
            node.setDocumentName(documentName);
          }
        }
       
        xres.setContent(out.toString());
        policyCollection.storeResource(xres);
        node.commit(true);
      }
      catch (final XMLDBException e)
      {
        final StringBuffer message = new StringBuffer();
        message.append("Error saving policy '");
        message.append(node.getId());
        message.append("' ");
        if(documentName != null)
        {
          message.append(" to document '");
          message.append(documentName);
          message.append("' ");
        }
        ClientFrame.showErrorMessage(message.toString(), e);
      }
    }
    if(removeDocs == null)
      {return;}
    for(final String documentName : removeDocs)
    {
      try
      {
        final Resource removeResource = policyCollection.getResource(documentName);
        policyCollection.removeResource(removeResource);
      }
      catch (final XMLDBException xe)
      {
        LOG.warn("Could not remove resource '" + documentName + "'", xe);
View Full Code Here

    {
      final String[] resourceIds = policyCollection.listResources();
      for(int i = 0; i < resourceIds.length; ++i)
      {
        final String resourceId = resourceIds[i];
        final Resource resource = policyCollection.getResource(resourceId);
        if(resource != null && resource instanceof XMLResource)
          {handleResource((XMLResource)resource, root);}
      }
    }
    catch (final XMLDBException xe)
View Full Code Here

                 
                  XMLDBAbstractCollectionManipulator.createCollection(root, path);
                 
                } else {

                    Resource resource;
                   
                File file = new File(path);
                name = file.getName();
                path = file.getParent();
               
                Collection target = (path==null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
               
                  MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
                 
                try{
                  NodeValue content = ModuleUtils.streamToXML(context, new ByteArrayInputStream(baos.toByteArray()));
                  resource = target.createResource(name, "XMLResource");
                  ContentHandler handler = ((XMLResource)resource).setContentAsSAX();
                  handler.startDocument();
                  content.toSAX(context.getBroker(), handler, null);
                  handler.endDocument();
                } catch(SAXException e){
                  resource = target.createResource(name, "BinaryResource");
                  resource.setContent(baos.toByteArray());
                    }
               
                if (resource != null){
                  if (mime != null){
                    ((EXistResource)resource).setMimeType(mime.getName());
View Full Code Here

TOP

Related Classes of org.xmldb.api.base.Resource

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.