⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cm.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		LOG.debug("Cm cannot write doc", e);
            throw e;
        } finally {
            if (op != null) {
                try {
                    op.close();
                } catch (IOException e) {
                }
            }
        }
    }
    
    /**
     * saves a StructuredDocument in specified dir, and file name from a input stream
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  source           Description of Parameter
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn, 
                          String fn, 
			  InputStream source) throws IOException {
        saveBytes(dn, fn, source, DiscoveryService.INFINITE_LIFETIME, DiscoveryService.NO_EXPIRATION );
    }
    
    
    /**
     * Saves in specified dir, and file name from a input stream
     *
     * @param  dn               directory name
     * @param  fn               file name
     * @param  source           Description of Parameter
     * @param  timeoutInMyCache time after which the document is removed from
     * the local cahce. Expressed in milliseconds relative to to now.
     * @param  timeoutForOthers  time after which the document is removed from
     * remote caches. Expressed in milliseconds relative to to now.
     * @exception  IOException  if an I/O error occurs
     * @since 1.0
     */
    public void saveBytes(String dn,
                          String fn,
			  InputStream source,
			  long timeoutInMyCache,
			  long timeoutForOthers) throws IOException {
        
        File file = getFile(dn, fn);
        FileOutputStream op = null;
        byte[] buffer;
        
        // ???? bondolo@jxta.org 20020427 shouldn't we be removing the old version of the file from index?
        
        try {
            expirer.scheduleFileExpiration(file,
            timeoutInMyCache,
            timeoutForOthers );
            
            op = new FileOutputStream(file);
            buffer = new byte[4096];
            
            do {
                int res = source.read(buffer);
                
                if (res == 0) {
                    continue;
                } else if (res == -1) {
                    break;
                }
                op.write(buffer, 0, res);
            } while (true);
            
            op.flush();
            op.close();
            op = null;
            source.close();
            
            // reopen the file we just saved for the indexer.
            source = new FileInputStream( file );
            
            if (searchMethod == CACHE) {
                ((CmCache) caches.get(dn)).add(fn);
            }
            
        } catch (IOException e) {
            if (LOG.isEnabledFor(Priority.WARN))
                LOG.warn("Cm cannot write doc", e);
            throw e;
        } finally {
            if (source != null) {
                try {
                    source.close();
                    source = null;
                } catch (IOException e) {
                }
            }
            
            if (op != null) {
                try {
                    op.close();
                    op = null;
                } catch (IOException e) {
                }
            }
        }
    }
    
    
    /**
     *  Delete a folder and all of its contents.
     *
     *  @param  dn               direcory name to be deleted
     *  @return true if the item was deleted otherwise false. some of the items
     *  may be deleted when false is returned.
     *
     * @exception  IOException  if an I/O error occurs
     */
    public boolean cleanupFolder(String dn) throws IOException {
        
        boolean deleted = true;
        
        if (null == dn) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug("cleanupFolder : dir cannot be null" );
            throw new IllegalArgumentException( "dir cannot be null" );
        }
        
        if (LOG.isDebugEnabled()) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug(" cleaning up " + dn);
        }
        
        File dir = new File(rootDir, dn);
        String[] files = getFileNames(dn);
        
        for (int i = 0; files != null && i < files.length; ++i) {
            File file = new File(dir, files[i]);
            
            if( file.isDirectory() ) {
                deleted &= cleanupFolder( file.getPath() );
            } else
                expirer.cancelFileExpiration( file );
            
            if ( file.exists() )
                deleted &= file.delete();
        }
        
        if( !deleted ) {
            if (LOG.isEnabledFor(Priority.WARN))
                LOG.warn("failed removing : " + dir.getCanonicalPath() );
        }
        
        return deleted;
    }
    
    
    /**
     * Search and recovers documents that contains at least
     * a mathing pair of tag/value.
     *
     * @since 1.0
     *
     * @param  dn     contains the name of the folder on which to perform the search
     * @param  tag    contains the name of the tag on which to search on
     * @param  value  contains the value to search on.
     * @return        Enumeration containing of all the documents that
     * have been found.
     **/
    public Enumeration search(String dn, String tag, String value) {
        
        if( (null == tag) || (tag.length() == 0) ) {
            throw new IllegalArgumentException( "tag must be non-null and non-empty" );
        }
        
        if( (null == value) || (value.length() == 0) ) {
            throw new IllegalArgumentException( "value must be non-null and non-empty" );
        }
        
        Vector res = new Vector();
        
        if (searchMethod == CACHE) {
            CmCache cmc = (CmCache) caches.get(dn);
            if (cmc.containsKey(tag)) {
                return cmc.query(tag, value);
            }
        }
        
        // if we're here it is because CmCache did not cache the attr we
        // interested in
        
        String[] files = getFileNames(dn);
        
        if (files == null) {
            return res.elements();
        }
        
        for (int i = 0; i < files.length; i++) {
            if (searchFile(dn, files[i], tag, value)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("found a hit " + files[i]);
                }
                res.addElement(files[i]);
            }
        }
        
        return res.elements();
    }
    
    /**
     *  Returns a file descriptor matching the directory and file provided.
     *
     * @param  dn  the relative dir name
     * @param  fn  the leaf file name
     * @return     File    File Object for the file
     * @since 1.0
     */
    File getFile(String dn, String fn) {
        File d = new File(rootDir, dn);
        
        if (!d.exists()) {
            d.mkdirs();
        }
        return new File(d, fn);
    }
    
    
    /**
     *  Restore a saved StructuredDocument.
     *
     * @param  file the file to restore
     * @return  StructuredDocument containing the file
     * @throws  IOException if an I/O error occurs
     * @since 1.0
     */
    StructuredDocument restoreFile(File file) throws IOException {
        
        if (LOG.isDebugEnabled()) {
            LOG.debug("restore document from " + file);
        }
        
        InputStream ip = null;
        StructuredDocument doc = null;
        
        try {
            ip = new FileInputStream(file);
            doc = StructuredDocumentFactory.newStructuredDocument
            (new MimeMediaType("text/xml"), ip);
        } finally {
            if (ip != null) {
                try {
                    ip.close();
                } catch (IOException e) {
                }
            }
        }
        
        return doc;
    }
    
    /**
     * Search a file for a specified tag/value. matching is done by ignoring
     *  case sensitivity.
     *
     * @param  dn     directory name
     * @param  fn     file name
     * @param  tag    element value
     * @param  value  value of the element to search for
     * @return        true if the file does contain tag/value
     * @since 1.0
     */
    private boolean searchFile(String dn,
                               String fn,
			       String tag,
			       String value) {
        
        if( (null == tag) || (tag.length() == 0) ) {
            throw new IllegalArgumentException( "tag must be non-null and non-empty" );
        }
        
        if( (null == value) || (value.length() == 0) ) {
            throw new IllegalArgumentException( "value must be non-null and non-empty" );
        }
        
        value = value.toUpperCase();
        
        // to indicate whether  the wild card is at the beinging of the string
        // or at the end
        boolean startswith = false;
        boolean endswith   = false;
        boolean matchany   = false;
        
        if( "*".equals( value ) )
            matchany = true;
        else {
            if ( value.charAt(0) == '*' ) {
                endswith = true;
                value = value.substring( 1 );
            }
            
            if ( value.charAt( value.length()-1 ) == '*' ) {
                startswith = true;
                value = value.substring(0, value.length()-1 );
            }
            
            // check if value was "**"
            if( value.length() == 0 )
                matchany = true;
        }
        
        StructuredDocument doc = null;
        
        try {
            doc = restore(dn, fn);
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug("searchFile failed restoring doc: " + e);
            return false;
        }
        
        if (doc == null) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug("searchFile failed restoring doc: doc is null");
            return false;
        }
        
        try {
            Enumeration enum = doc.getChildren();
            if ( !enum.hasMoreElements() ) {
                if (LOG.isEnabledFor(Priority.DEBUG))
                    LOG.debug("searchFile failed restoring doc: doc has no elements");
                return false;
            }
            
            while (enum.hasMoreElements()) {
                Element e = (Element) enum.nextElement();
                
                Object etag = e.getKey();
                if( !(etag instanceof String) )
                    continue;
                
                if ( ((String)(etag)).equalsIgnoreCase(tag)) {
                    
                    // got the tag? maybe thats good enough
                    if ( matchany )
                        return true;
                    
                    Object val = e.getValue();
                    
                    if ( value.getClass().isInstance(val) ) {
                        
                        // convert both values to upper case prior to making
                        // the comparison
                        
                        String uVal = ((String) val).toUpperCase();
                        
                        if ( startswith && !endswith ) {
                            return (uVal.startsWith(value));
                        } else if (endswith && !startswith ) {
                            return (uVal.endsWith(value) );
                        } else if (startswith && endswith ) {
                            return (uVal.indexOf(value) >= 0);
                        } else {
                            return (uVal.equals(value));
                        }
                    }
                }
            }
        } catch (Exception e) {
            if (LOG.isEnabledFor(Priority.DEBUG))
                LOG.debug("searchFile has failed: ", e);
            return false;
        }
        return false;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -