📄 sampleresourceindexer.java
字号:
// SampleResourceIndexer.java// $Id: SampleResourceIndexer.java,v 1.22 2002/10/29 14:39:02 ylafon Exp $ // (c) COPYRIGHT MIT and INRIA, 1997.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.tools.resources.indexer;import java.util.Enumeration;import java.util.Hashtable;import java.util.NoSuchElementException;import java.util.Vector;import java.io.File;import org.w3c.tools.resources.Attribute;import org.w3c.tools.resources.AttributeHolder;import org.w3c.tools.resources.AttributeRegistry;import org.w3c.tools.resources.ContainerInterface;import org.w3c.tools.resources.ContainerResource;import org.w3c.tools.resources.DummyResourceReference;import org.w3c.tools.resources.FramedResource;import org.w3c.tools.resources.InvalidResourceException;import org.w3c.tools.resources.MultipleLockException;import org.w3c.tools.resources.RequestInterface;import org.w3c.tools.resources.Resource;import org.w3c.tools.resources.ResourceContext;import org.w3c.tools.resources.ResourceFrame;import org.w3c.tools.resources.ResourceReference;import org.w3c.tools.resources.ServerInterface;import org.w3c.tools.resources.StringArrayAttribute;import org.w3c.tools.resources.StringAttribute;class SampleIndexerEnumeration implements Enumeration { private static final String list[] = { "directories".intern(), "extensions".intern() }; int idx = 0; public boolean hasMoreElements() { return idx < list.length; } public Object nextElement() { if ( idx >= list.length ) throw new NoSuchElementException("SampleResourceIndexer enum"); return list[idx++]; } SampleIndexerEnumeration() { this.idx = 0; }}/** * A container for directories and templates. */public class SampleResourceIndexer extends Resource implements ContainerInterface, ResourceIndexer{ private static final boolean debug = false; protected static final boolean extCaseSensitive = false; protected static final String defname = "*default*"; private static final String harmfulNames[] = { "aux".intern(), "con".intern() }; static public boolean isWinPlatform = (File.pathSeparatorChar == ';'); /** * Attribute index - the super indexer, if any. */ protected static int ATTR_SUPER_INDEXER = -1; /** * Attribute index - the super indexer, if any. */ protected static int ATTR_NOT_INDEXED = -1; static { Attribute a = null; Class c = org.w3c.tools.resources.indexer.SampleResourceIndexer.class; // Our super indexer: a = new StringAttribute("super-indexer" , null , Attribute.EDITABLE); ATTR_SUPER_INDEXER = AttributeRegistry.registerAttribute(c, a); a = new StringArrayAttribute("not-indexed-names" , null , Attribute.EDITABLE); ATTR_NOT_INDEXED = AttributeRegistry.registerAttribute(c, a); } protected ResourceReference directories = null; protected ResourceReference extensions = null; protected ResourceReference contentTypes = null; protected synchronized ResourceReference getDirectories() { if ( directories == null ) { String diridxid = getIdentifier()+"-d"; directories = new DummyResourceReference( new TemplateContainer( new ResourceContext(getContext()), diridxid+".db")); } return directories; } protected synchronized ResourceReference getExtensions() { if ( extensions == null ) { String extidxid = getIdentifier()+"-e"; extensions = new DummyResourceReference( new TemplateContainer( new ResourceContext(getContext()), extidxid+".db")); } return extensions; } public long lastModified() { return getLong(ATTR_LAST_MODIFIED, -1); } public String getSuperIndexer() { return getString(ATTR_SUPER_INDEXER, null); } public Enumeration enumerateResourceIdentifiers(boolean all) { return new SampleIndexerEnumeration(); } public ResourceReference lookup(String name) { if ( name.equals("directories") ) { return getDirectories(); } else if ( name.equals("extensions") ) { return getExtensions(); } return null; } /** * Delete this inexer * @exception org.w3c.tools.resources.MultipleLockException if someone * else has locked the indexer. */ public synchronized void delete() throws MultipleLockException { // Remove the two stores we are responsible for: DummyResourceReference rr = (DummyResourceReference) getExtensions(); try { Resource r = rr.lock(); r.delete(); } catch (InvalidResourceException ex) { } finally { rr.invalidate(); rr.unlock(); } rr = (DummyResourceReference) getDirectories(); try { Resource r = rr.lock(); r.delete(); } catch (InvalidResourceException ex) { } finally { rr.invalidate(); rr.unlock(); } super.delete(); } public void delete(String name) { throw new RuntimeException("static container"); } public void registerResource(String name, Resource resource, Hashtable defs) { throw new RuntimeException("static container"); } /* * Load an extension descriptor. * @param ext The name of the extension. * @return An instance of Extension, or <strong>null</strong>. */ public synchronized ResourceReference loadExtension (String name) { ResourceReference rr = getExtensions(); ResourceReference ext = null; try { TemplateContainer exts = (TemplateContainer) rr.lock(); // try with exact and if it fails, try to with lower case ext = exts.lookup(name); if (ext == null && !extCaseSensitive) return exts.lookup(name.toLowerCase()); return ext; } catch (InvalidResourceException ex) { String msg = ("[resource indexer]: extensions \""+ name+ "\" couldn't be restored ("+ex.getMessage()+")"); getContext().getServer().errlog(msg); return null; } finally { rr.unlock(); } } /** * Return the class (if any) that our store defines for given extension. * @param ext The extension we want a class for. * @return A Class instance, or <strong>null</strong>. */ protected ResourceReference getTemplateFor(String ext) { ResourceReference rr = loadExtension(ext) ; if (rr != null) { try { Resource template = rr.lock(); if (template != null) { Resource check = new Resource(); if (template.getClass() == check.getClass()) return null; else return rr; } return null; } catch (InvalidResourceException ex) { return null; } finally { rr.unlock(); } } return null; } /** * Merge the attributes this extension defines, with the provided ones. * @param attrs The attributes we want to fill with default values. * @param ext The extension name. * @param into The already built set of default values. * @return A Hashtable, containing the augmented set of default attribute * values. */ protected Hashtable mergeDefaultAttributes(Resource template, String ext, Hashtable into) { Attribute attrs[] = template.getAttributes(); ResourceReference rr = loadExtension(ext) ; if (rr != null) { try { Resource e = rr.lock() ; if ( e != null ) { for (int i = 0 ; i < attrs.length ; i++) { if ( ! template.definesAttribute(i) ) { int idx = e.lookupAttribute(attrs[i].getName()); if ( idx >= 0 ) { Object value = e.getValue(idx, null); if ( value != null ) into.put(attrs[i].getName(), value) ; } } } } return into ; } catch (InvalidResourceException ex) { return null; } finally { rr.unlock(); } } return null; } /** * Merge the attributes this extension defines, with the provided ones. * @param origFrame The original frame * @param ext The extension name * @param into The ResourceReference of the frame to be merged */ protected void mergeFrameAttributes(ResourceFrame origFrame, String ext, ResourceReference frameref) { int idx; Object oldval, newval; String atname; try { Resource frame = frameref.lock(); Attribute attrs[] = frame.getAttributes(); for (int i = 0 ; i < attrs.length ; i++) { atname = attrs[i].getName(); try { oldval = origFrame.getValue(atname, null); if (oldval == null) { // not defined, try to merge try { origFrame.setValue(atname, frame.getValue(atname, null)); } catch (Exception ex) { // undefined value, will stay null } } else if (atname.equals("quality")) { // small hack here, quality factor are merged // should be removed by a quality per encoding Double d = (Double)frame.getValue(atname, null); if (d != null) { d = new Double(d.doubleValue() * ((Double) oldval).doubleValue()); origFrame.setValue(atname, d); } } } catch (Exception undefined) { // attribute is NOT defined } } } catch (InvalidResourceException ex) { ex.printStackTrace(); } finally { frameref.unlock(); } } /** * Get this name's extensions. * @param name The file name. * @return An array of string, giving ach (parsed) extension, or * <strong>null</strong> if none was found. */ private final static String noextension[] = { "*noextension*" } ; protected String[] getFileExtensions(String name) { Vector items = new Vector() ; int dpos = name.indexOf ('.') ; if ( dpos > 0 ) { int pos = dpos+1 ; while ( (dpos = name.indexOf ('.', pos)) != -1 ) { // Skip empty extension: if ( dpos == pos+1 ) { pos++ ; continue ; } // Else add new extension: items.addElement (name.substring(pos, dpos)) ; pos = dpos + 1; } if ( pos < name.length() ) items.addElement (name.substring(pos)) ; String exts[] = new String[items.size()] ; items.copyInto (exts) ; return exts ; } else { // That file has no extensions, we'll use '.' as its extension return noextension; } } /** * Create a default file resource for this file (that exists). * @param directory The directory of the file. * @param name The name of the file. * @param defs A set of default attribute values. * @return An instance of Resource, or <strong>null</strong> if * we were unable to create it. */ protected Resource createFileResource(File directory, RequestInterface req, String name, Hashtable defs) { ResourceReference rr = null; FramedResource template = null; Resource newres = null; Class proto = null; try { proto = Class.forName("org.w3c.tools.resources.ProtocolFrame"); } catch (Exception ex) { // fatal error! return null; } // Check that at least one class is defined for all the extensions: String exts[] = getFileExtensions(name) ; if ( exts == null ) return null ; for (int i = exts.length-1 ; i >= 0 ; i--) { rr = getTemplateFor(exts[i]) ; if ( rr != null ) break ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -