📄 cm.java
字号:
/**
* 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) {
Vector res = new Vector();
if ( searchMethod == INDEX && (value.indexOf("*") == -1 )) {
String[] files = searchIndex(dn, tag, value);
if (files == null) {
return res.elements();
} else if (files.length > 0) {
for (int i = 0; i < files.length; i++) {
if (null != files[i]) {
res.addElement(files[i]);
}
else if (LOG.isEnabledFor(Priority.WARN))
LOG.warn("There was a null element in the index results");
}
return res.elements();
}
} else 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()) {
if (LOG.isEnabledFor(Priority.DEBUG))
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();
expirer.addDocDir(d);
}
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()) {
if (LOG.isEnabledFor(Priority.DEBUG)) 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;
}
/**
* Convenience method; creates a dir if does not exist.
*
* @param dirName directory name
* @return File
* @exception IOException if an I/O error occurs
* @since 1.0
*/
private File initDir(String dirName)
throws IOException {
File dir = new File(rootDir, dirName);
if (!dir.exists()) {
// We need to create the directory
if (!dir.mkdirs()) {
throw new IOException("Cm cannot create directory " + dir);
}
}
//PDA requirements 21.02.2002
//package net.jxta.impl.index.* is not ported to PDA
//class net.jxta.impl.indexIndexServiceImpl is excluded for the time being
/*if (searchMethod == INDEX) {
if (!indexes.containsKey(dirName)) {
try {
indexes.put(dirName,
indexService.getIndex(new File
(dir.getAbsolutePath(),
indexFilename)));
if (LOG.isEnabledFor(Priority.INFO)) LOG.info("Creating index " + dirName);
} catch (IOException e) {
if (LOG.isDebugEnabled()) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("Failed to create index " + e);
}
}
}
}*/
//PDA requirements 21.02.2002
return dir;
}
/**
* searchs the index for a match
*
* @param dn directory name
* @param tag element value
* @param value value of the element to search for
* @return String array of matching file names
* @since 1.0
*/
private String[] searchIndex(String dn, String tag, String value) {
if (searchMethod != INDEX) {
return null;
}
//PDA requirements 21.02.2002
//package net.jxta.impl.index.* is not ported to PDA
//search method "INDEX" is not used on a PDA for the time being
/*if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("Looking for " + tag + " = " + value +
" in " + dn + "Rootdir = " + rootDir.toString());
String query = "//[" + tag + "='" + value + "']";
try {
IndexService.Index index =((IndexService.Index) indexes.get(dn));
String[] results = index.query(query);
return results;
} catch (IndexService.BadQueryException e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("bad query expression ", e);
} catch (IndexService.IndexException e) {
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn("bad index", e);
}*/
//PDA requirements 21.02.2002
return null;
}
/**
* adds a document to the index
*
* @param dn directory name
* @param name file name
* @param doc document to index
* @since 1.0
*/
private synchronized void indexFile(
String dirname,
String name,
StructuredDocument doc) {
//PDA requirements 21.02.2002
//package net.jxta.impl.index.* is not ported to PDA
//search method "INDEX" is not used on a PDA for the time being
/*if (searchMethod != INDEX) {
return;
}
try {
((IndexService.Index) indexes.get(dirname)).add(name, doc.getStream());
} catch (IndexService.BadDocumentException e) {
// XXXX do nothing right now
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "Index : Bad Document", e );
} catch (IndexService.IndexException e) {
// XXXX do nothing right now
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "Index : Index Exception", e );
} catch (IOException e) {
// XXXX do nothing right now
if (LOG.isEnabledFor(Priority.WARN)) LOG.warn( "Index : IO Failure", e );
}*/
//PDA requirements 21.02.2002
}
/**
* Search a file for a specified tag/value
*
* @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) {
// to indicate whether the wild card is at the beinging of the string
// or at the end
boolean startswith = false;
boolean endswith = false;
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 {
Element e = null;
Enumeration enum = doc.getChildren();
if ((enum == null) || !enum.hasMoreElements()) {
if (LOG.isEnabledFor(Priority.DEBUG))
LOG.debug("searchFile failed restoring doc: doc has no element");
return false;
}
String uTag = tag.toUpperCase();
while (enum.hasMoreElements()) {
e = (Element) enum.nextElement();
if (((String)(e.getKey())).toUpperCase().equals(uTag)) {
Object val = e.getValue();
if (value.getClass().isInstance(val)) {
if( value.length() < 1 )
continue;
if (value.charAt(0) == '*') {
endswith = true;
value = value.substring(1, value.length());
}
if (value.charAt(value.length()-1) == '*') {
startswith =true;
value = value.substring(0, value.indexOf("*"));
}
// '*' by itself not allowed
if (value.length() == 0) {
return false;
}
// convert both values to upper case prior to making
// the comparison
String uVal = ((String) val).toUpperCase();
String uValue = ((String) value).toUpperCase();
if (startswith && ! endswith ) {
return (uVal.startsWith(uValue));
} else if (endswith && !startswith ) {
return (uVal.endsWith(uValue) );
} else if (startswith && endswith ) {
return (uVal.indexOf(uValue) >= 0);
} else {
return (uVal.equals(uValue));
}
}
}
}
} catch (Exception e) {
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug("searchFile has failed: ", e);
return false;
}
return false;
}
/**
* Expiration Listener Interface
*
*/
public interface ExpirationListener {
/**
*
* Expiration event
*
* @param dn directory name
* @param fn file name
* @param timeout Description of Parameter
* @since 1.0
*/
public void expired(String dn, String fn, long timeout);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -