softreferencegrammarpool.java
来自「JAVA 所有包」· Java 代码 · 共 433 行 · 第 1/2 页
JAVA
433 行
/* * Copyright 2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.jaxp.validation;import java.lang.ref.Reference;import java.lang.ref.ReferenceQueue;import java.lang.ref.SoftReference;import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;import com.sun.org.apache.xerces.internal.xni.grammars.XMLSchemaDescription;import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;/** * <p>This grammar pool is a memory sensitive cache. The grammars * stored in the pool are softly reachable and may be cleared by * the garbage collector in response to memory demand. Equality * of <code>XMLSchemaDescription</code>s is determined using both * the target namespace for the schema and schema location.</p> * * @author Michael Glavassevich, IBM * @version $Id: SoftReferenceGrammarPool.java,v 1.1.4.1 2005/09/05 11:43:19 sunithareddy Exp $ */final class SoftReferenceGrammarPool implements XMLGrammarPool { // // Constants // /** Default size. */ protected static final int TABLE_SIZE = 11; /** Zero length grammar array. */ protected static final Grammar [] ZERO_LENGTH_GRAMMAR_ARRAY = new Grammar [0]; // // Data // /** Grammars. */ protected Entry [] fGrammars = null; /** Flag indicating whether this pool is locked */ protected boolean fPoolIsLocked; /** The number of grammars in the pool */ protected int fGrammarCount = 0; /** Reference queue for cleared grammar references */ protected final ReferenceQueue fReferenceQueue = new ReferenceQueue(); // // Constructors // /** Constructs a grammar pool with a default number of buckets. */ public SoftReferenceGrammarPool() { fGrammars = new Entry[TABLE_SIZE]; fPoolIsLocked = false; } // <init>() /** Constructs a grammar pool with a specified number of buckets. */ public SoftReferenceGrammarPool(int initialCapacity) { fGrammars = new Entry[initialCapacity]; fPoolIsLocked = false; } // // XMLGrammarPool methods // /* <p> Retrieve the initial known set of grammars. This method is * called by a validator before the validation starts. The application * can provide an initial set of grammars available to the current * validation attempt. </p> * * @param grammarType The type of the grammar, from the * <code>com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription</code> * interface. * @return The set of grammars the validator may put in its "bucket" */ public Grammar [] retrieveInitialGrammarSet (String grammarType) { synchronized (fGrammars) { clean(); // Return no grammars. This allows the garbage collector to sift // out grammars which are not in use when memory demand is high. // It also allows the pool to return the "right" schema grammar // based on schema locations. return ZERO_LENGTH_GRAMMAR_ARRAY; } } // retrieveInitialGrammarSet (String): Grammar[] /* <p> Return the final set of grammars that the validator ended up * with. This method is called after the validation finishes. The * application may then choose to cache some of the returned grammars.</p> * <p>In this implementation, we make our choice based on whether this object * is "locked"--that is, whether the application has instructed * us not to accept any new grammars.</p> * * @param grammarType The type of the grammars being returned; * @param grammars An array containing the set of grammars being * returned; order is not significant. */ public void cacheGrammars(String grammarType, Grammar[] grammars) { if (!fPoolIsLocked) { for (int i = 0; i < grammars.length; ++i) { putGrammar(grammars[i]); } } } // cacheGrammars(String, Grammar[]); /* <p> This method requests that the application retrieve a grammar * corresponding to the given GrammarIdentifier from its cache. * If it cannot do so it must return null; the parser will then * call the EntityResolver. </p> * <strong>An application must not call its EntityResolver itself * from this method; this may result in infinite recursions.</strong> * * This implementation chooses to use the root element name to identify a DTD grammar * and the target namespace to identify a Schema grammar. * * @param desc The description of the Grammar being requested. * @return The Grammar corresponding to this description or null if * no such Grammar is known. */ public Grammar retrieveGrammar(XMLGrammarDescription desc) { return getGrammar(desc); } // retrieveGrammar(XMLGrammarDescription): Grammar // // Public methods // /** * Puts the specified grammar into the grammar pool and associates it to * its root element name or its target namespace. * * @param grammar The Grammar. */ public void putGrammar(Grammar grammar) { if (!fPoolIsLocked) { synchronized (fGrammars) { clean(); XMLGrammarDescription desc = grammar.getGrammarDescription(); int hash = hashCode(desc); int index = (hash & 0x7FFFFFFF) % fGrammars.length; for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) { if (entry.hash == hash && equals(entry.desc, desc)) { if (entry.grammar.get() != grammar) { entry.grammar = new SoftGrammarReference(entry, grammar, fReferenceQueue); } return; } } // create a new entry Entry entry = new Entry(hash, index, desc, grammar, fGrammars[index], fReferenceQueue); fGrammars[index] = entry; fGrammarCount++; } } } // putGrammar(Grammar) /** * Returns the grammar associated to the specified grammar description. * Currently, the root element name is used as the key for DTD grammars * and the target namespace is used as the key for Schema grammars. * * @param desc The Grammar Description. */ public Grammar getGrammar(XMLGrammarDescription desc) { synchronized (fGrammars) { clean(); int hash = hashCode(desc); int index = (hash & 0x7FFFFFFF) % fGrammars.length; for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) { Grammar tempGrammar = (Grammar) entry.grammar.get(); /** If the soft reference has been cleared, remove this entry from the pool. */ if (tempGrammar == null) { removeEntry(entry); } else if ((entry.hash == hash) && equals(entry.desc, desc)) { return tempGrammar; } } return null; } } // getGrammar(XMLGrammarDescription):Grammar /** * Removes the grammar associated to the specified grammar description from the * grammar pool and returns the removed grammar. Currently, the root element name * is used as the key for DTD grammars and the target namespace is used * as the key for Schema grammars. * * @param desc The Grammar Description. * @return The removed grammar. */ public Grammar removeGrammar(XMLGrammarDescription desc) { synchronized (fGrammars) { clean(); int hash = hashCode(desc); int index = (hash & 0x7FFFFFFF) % fGrammars.length; for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?