📄 sampleresourceindexer.java
字号:
} if ( rr == null ) { // Look for a default template: if ((rr = loadExtension(defname)) == null) return null ; } // Create the runtime-time default values for attributes. if ( defs == null ) { defs = new Hashtable(5) ; } String s_dir = "directory".intern(); String s_ide = "identifier".intern(); String s_fil = "filename".intern(); String s_con = "context".intern(); if ( defs.get(s_dir) == null ) { defs.put(s_dir, directory) ; } if ( defs.get(s_ide) == null ) { defs.put(s_ide, getIndexedFileName(name)) ; } else { defs.put(s_ide, getIndexedFileName((String)defs.get(s_ide))) ; } if ( defs.get(s_fil) == null) { defs.put(s_fil, name) ; } if ( defs.get(s_con) == null ) { defs.put(s_con, getContext()); } try { template = (FramedResource) rr.lock(); if (exts != null) { // Merge with values defined by the extension: for (int i = exts.length ; --i >= 0 ; ) mergeDefaultAttributes(template, exts[i], defs) ; } // Create, initialize and return the new resouce try { newres = (FramedResource) template.getClone(defs); } catch (Exception ex) { ex.printStackTrace() ; return null ; } } catch (InvalidResourceException ex) { ex.printStackTrace(); return null; } finally { rr.unlock(); } // clone has been done, merge frames now if (exts != null) { ResourceFrame rf[] = newres.collectFrames(proto); if (rf != null) { for (int j=0; j < rf.length; j++) { for (int i = exts.length-1 ; i >= 0 ; i--) { rr = getTemplateFor(exts[i]) ; if ( rr != null ) { FramedResource fr = null; try { fr = (FramedResource) rr.lock(); ResourceReference trr = null; trr = fr.getFrameReference(proto); if (trr != null) { mergeFrameAttributes(rf[j], exts[i], trr); } } catch (InvalidResourceException iex) { iex.printStackTrace(); return null; } finally { rr.unlock(); } } } } } } return newres; } /** * Load a given directory template from the store. * @param name The name of the template to load. * @return An instance of ResourceReference, or <strong>null</strong>. */ public synchronized ResourceReference loadDirectory(String name) { ResourceReference rr = getDirectories(); try { TemplateContainer dirs = (TemplateContainer) rr.lock(); return dirs.lookup(name); } catch (InvalidResourceException ex) { // Emit an error message, and remove it ! String msg = ("[resource indexer]: directory template \""+ name + "\" couldn't be restored. It has "+ "been removed."); getContext().getServer().errlog(msg); return null; } finally { rr.unlock(); } } /** * Create a default container resource for this directory (that exists). * @param directory The parent directory. * @param req the request that triggered this creation * @param name The name of its sub-directory to index. * @param defaults A set of default atribute values. * @return A Resource instance, or <strong>null</strong> if * the indexer was unable to build a default resource for the directory. */ protected Resource createDirectoryResource(File directory, RequestInterface req, String name, Hashtable defs) { // Lookup the directory path, for an existing template. File dir = new File(directory, name) ; Resource dirtempl = null; ResourceReference rr = null; rr = loadDirectory(name); // If no template available, default to a raw DirectoryResource if ((rr == null) && ((rr=loadDirectory(defname)) == null)) return null; try { dirtempl = rr.lock(); // Clone the appropriate template: if ( defs == null ) { defs = new Hashtable(3); } String s_dir = "directory".intern(); String s_ide = "identifier".intern(); if ( defs.get(s_dir) == null ) { defs.put(s_dir, directory) ; } if ( defs.get(s_ide) == null ) { defs.put(s_ide, getIndexedDirName(name)) ; } else { defs.put(s_ide, getIndexedDirName((String)defs.get(s_ide))) ; } //FIXME context ??? // if ( defs.get("context") == null ) // defs.put("context", getContext()); try { return (Resource) dirtempl.getClone(defs); } catch (Exception ex) { ex.printStackTrace() ; return null ; } } catch (InvalidResourceException ex) { ex.printStackTrace(); return null; } finally { rr.unlock(); } } /** * Try to create a virtual resource if the real (physical) resource * is not there. * @param directory The directory the file is in. * @param req the request that triggered this creation * @param name The name of the file. * @param defs Any default attribute values that should be provided * to the created resource at initialization time. * @return A Resource instance, or <strong>null</strong> if the given * file can't be truned into a resource given our configuration * database. */ protected Resource createVirtualResource( File directory, RequestInterface req, String name, Hashtable defs) { ResourceReference rr = null; Resource dirtempl = null; rr = loadDirectory(name); if (rr != null) { try { dirtempl = rr.lock(); String classname = dirtempl.getClass().getName().intern(); String idr = "org.w3c.jigsaw.resources.DirectoryResource".intern(); if (classname == idr) { File file = new File(directory, name) ; // check in this case that we will have a special // configuration ONLY for a real directory if (!file.exists()) { return null; } if (!file.isDirectory()) { return null; } } String ifr = "org.w3c.tools.resources.FileResource".intern(); if (classname == ifr) { File file = new File(directory, name) ; // check that we won't override a bad resource type if (!file.exists()) { return null; } if (file.isDirectory()) { return null; } } if ( defs == null ) { defs = new Hashtable(4); } String s_dir = "directory".intern(); String s_ide = "identifier".intern(); String s_con = "context".intern(); if ( defs.get(s_dir) == null ) { defs.put(s_dir, directory) ; } if ( defs.get(s_ide) == null ) { defs.put(s_ide, name) ; } if ( defs.get(s_con) == null ) { defs.put(s_con, getContext()); } try { return (Resource) dirtempl.getClone(defs); } catch (Exception ex) { ex.printStackTrace() ; return null ; } } catch (InvalidResourceException ex) { ex.printStackTrace(); return null; } finally { rr.unlock(); } } return null; } /** * Try to create a resource for the given file. * This method makes its best efforts to try to build a default * resource out of a file. * @param directory The directory the file is in. * @param name The name of the file. * @param defs Any default attribute values that should be provided * to the created resource at initialization time. * @return A Resource instance, or <strong>null</strong> if the given * file can't be truned into a resource given our configuration * database. */ public Resource createResource(ContainerResource container, RequestInterface request, File directory, String name, Hashtable defs) { if (isWinPlatform) { for (int i=0; i < harmfulNames.length; i++) { if (name.equalsIgnoreCase(harmfulNames[i])) { return null; } } } // if it matches the Not-Indexed list, then exit String[] removed = (String[]) getValue(ATTR_NOT_INDEXED, null); if (removed != null) { for (int i=0; i < removed.length; i++) { if (name.equals(removed[i])) { return null; } } } // Does this file exists ? File file = new File(directory, name) ; Resource result = null; result = createVirtualResource(directory, request, name, defs); if (result != null) { return result; } if (!file.exists()) { return null; } // Okay, dispatch on wether it is a file or a directory. if ( file.isDirectory() ) { result = createDirectoryResource(directory, request, name, defs) ; } else if ( file.isFile() ) { result = createFileResource(directory, request, name, defs) ; } else { // not a directory and not a real file, perhaps something not // really wanted return null; } if ( result != null ) return result; // Try the super indexer if available: String superIndexer = getSuperIndexer(); if ( superIndexer == null ) return null; IndexerModule m = null; m = (IndexerModule) getContext().getModule(IndexerModule.NAME); ResourceReference rri = m.getIndexer(superIndexer); if (rri == null) return null; try { ResourceIndexer p = (ResourceIndexer)rri.lock(); return ((p != null) ? p.createResource(container, request, directory, name, defs) : null); } catch (InvalidResourceException ex) { return null; } finally { rri.unlock(); } } /** * Get the name of the resource relative to the given filename. * @param name The name of the file. * @return a String, the resource name. */ public String getIndexedName(File directory, String name) { if (isWinPlatform) { for (int i=0; i < harmfulNames.length; i++) { if (name.equalsIgnoreCase(harmfulNames[i])) { return null; } } } File file = new File(directory, name); if (! file.exists()) return null; if (file.isDirectory()) return getIndexedDirName(name); //make sure that we will index this file (or directory) String exts[] = getFileExtensions(name) ; ResourceReference rr = null; if ( exts == null ) return null; for (int i = exts.length-1 ; i >= 0 ; i--) { rr = getTemplateFor(exts[i]) ; if ( rr != null ) break ; } if (rr != null) return getIndexedFileName(name); //try the super indexer String superIndexer = getSuperIndexer(); if ( superIndexer == null ) return null; IndexerModule m = null; m = (IndexerModule) getContext().getModule(IndexerModule.NAME); ResourceReference rri = m.getIndexer(superIndexer); if (rri == null) return null; try { ResourceIndexer p = (ResourceIndexer)rri.lock(); return ((p != null) ? p.getIndexedName(directory, name) : null); } catch (InvalidResourceException ex) { return null; } finally { rri.unlock(); } } protected String getIndexedFileName(String name) { return name; } protected String getIndexedDirName(String name) { return name; } // FIXME tests public SampleResourceIndexer(ResourceContext ctxt) { Hashtable init = new Hashtable(3); String s_ide = "identifier".intern(); String s_con = "context".intern(); init.put(s_con, ctxt); init.put(s_ide, "default"); initialize(init); } public SampleResourceIndexer() { super(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -