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

📄 objectfactoryloader.java

📁 该源代码为一些通用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param name the name of the property
     * @param attribName the xml-attribute name to use later.
     * @param handlerClass the attribute handler class.
     * @throws ObjectDescriptionException if an error occured.
     */
    protected void handleAttributeDefinition(final String name, final String attribName, final String handlerClass)
        throws ObjectDescriptionException {
        final AttributeHandler handler = loadAttributeHandler(handlerClass);
        this.orderedNames.add(name);
        this.attributeDefinition.add(new AttributeDefinition(name, attribName, handler));
    }

    /**
     * Handles an element definition. This method gets called after the object definition
     * was started. The method will be called for every defined element property. Element
     * properties are used to describe complex objects.
     *
     * @param name the name of the property
     * @param element the xml-tag name for the child element.
     * @throws ObjectDescriptionException if an error occurs.
     */
    protected void handleElementDefinition(final String name, final String element)
        throws ObjectDescriptionException {
        this.orderedNames.add(name);
        this.propertyDefinition.add(new PropertyDefinition(name, element));
    }

    /**
     * Handles an lookup definition. This method gets called after the object definition
     * was started. The method will be called for every defined lookup property. Lookup properties
     * reference previously created object using the object's registry name.
     *
     * @param name the property name of the base object
     * @param lookupKey the register key of the referenced object
     * @throws ObjectDescriptionException if an error occured.
     */
    protected void handleLookupDefinition(final String name, final String lookupKey)
        throws ObjectDescriptionException {
        final LookupDefinition ldef = new LookupDefinition(name, lookupKey);
        this.orderedNames.add(name);
        this.lookupDefinitions.add(ldef);
    }

    /**
     * Finializes the object definition.
     *
     * @throws ObjectDescriptionException if an error occures.
     */
    protected void endObjectDefinition()
        throws ObjectDescriptionException {

        final PropertyDefinition[] propertyDefs = (PropertyDefinition[])
        this.propertyDefinition.toArray(new PropertyDefinition[0]);
        final LookupDefinition[] lookupDefs = (LookupDefinition[])
        this.lookupDefinitions.toArray(new LookupDefinition[0]);
        final AttributeDefinition[] attribDefs = (AttributeDefinition[])
        this.attributeDefinition.toArray(new AttributeDefinition[0]);
        final ConstructorDefinition[] constructorDefs = (ConstructorDefinition[])
        this.constructorDefinition.toArray(new ConstructorDefinition[0]);
        final String[] orderedNamesDefs = (String[])
        this.orderedNames.toArray(new String[0]);

        final GenericObjectFactory objectFactory = new GenericObjectFactory
            (this.target, this.registerName, constructorDefs,
                propertyDefs, lookupDefs, attribDefs, orderedNamesDefs);
        this.objectMappings.put(this.target, objectFactory);
    }

    /**
     * Handles a constructor definition. Only one constructor can be defined for
     * a certain object type. The constructor will be filled using the given properties.
     *
     * @param propertyName the property name of the referenced local property
     * @param parameterClass the parameter class for the parameter.
     */
    protected void handleConstructorDefinition(final String propertyName, final String parameterClass) {
        final Class c = loadClass(parameterClass);
        this.orderedNames.add(propertyName);
        this.constructorDefinition.add(new ConstructorDefinition(propertyName, c));
    }

    /**
     * Handles a manual mapping definition. The manual mapping maps specific
     * read and write handlers to a given base class. Manual mappings always
     * override any other definition.
     *
     * @param className the base class name
     * @param readHandler the class name of the read handler
     * @param writeHandler the class name of the write handler
     * @return true, if the mapping was accepted, false otherwise.
     * @throws ObjectDescriptionException if an unexpected error occured.
     */
    protected boolean handleManualMapping(final String className, final String readHandler, final String writeHandler)
        throws ObjectDescriptionException {

        if (!this.manualMappings.containsKey(className)) {
            final Class loadedClass = loadClass(className);
            this.manualMappings.put(loadedClass, new ManualMappingDefinition
                (loadedClass, readHandler, writeHandler));
            return true;
        }
        return false;
    }

    /**
     * Starts a multiplex mapping. Multiplex mappings are used to define polymorphic
     * argument handlers. The mapper will collect all derived classes of the given
     * base class and will select the corresponding mapping based on the given type
     * attribute.
     *
     * @param className the base class name
     * @param typeAttr the xml-attribute name containing the mapping key
     */
    protected void startMultiplexMapping(final String className, final String typeAttr) {
        this.baseClass = className;
        this.attributeName = typeAttr;
        this.multiplexEntries = new ArrayList();
    }

    /**
     * Defines an entry for the multiplex mapping. The new entry will be activated
     * when the base mappers type attribute contains this <code>typename</code> and
     * will resolve to the handler for the given classname.
     *
     * @param typeName the type value for this mapping.
     * @param className the class name to which this mapping resolves.
     * @throws ObjectDescriptionException if an error occurs.
     */
    protected void handleMultiplexMapping(final String typeName, final String className)
        throws ObjectDescriptionException {
        this.multiplexEntries.add
            (new MultiplexMappingEntry(typeName, className));
    }

    /**
     * Finializes the multiplexer mapping.
     *
     * @throws ObjectDescriptionException if an error occurs.
     */
    protected void endMultiplexMapping() throws ObjectDescriptionException {
        final MultiplexMappingEntry[] mappings = (MultiplexMappingEntry[])
        this.multiplexEntries.toArray(new MultiplexMappingEntry[0]);
        final Class c = loadClass(this.baseClass);
        this.multiplexMappings.put(c,
            new MultiplexMappingDefinition(c, this.attributeName, mappings));
        this.multiplexEntries = null;
    }

    /**
     * Loads an instantiates the attribute handler specified by the given
     * class name.
     *
     * @param attribute the attribute handlers classname.
     * @return the created attribute handler instance
     * @throws ObjectDescriptionException if the handler could not be loaded.
     */
    private AttributeHandler loadAttributeHandler(final String attribute)
        throws ObjectDescriptionException {

        final Class c = loadClass(attribute);
        try {
            return (AttributeHandler) c.newInstance();
        }
        catch (Exception e) {
            throw new ObjectDescriptionException
                ("Invalid attribute handler specified: " + attribute);
        }
    }

    /**
     * Checks, whether the factory has a description for the given class.
     *
     * @param c the class to be handled by the factory.
     * @return true, if an description exists for the given class, false otherwise.
     */
    public boolean isGenericHandler(final Class c) {
        return this.objectMappings.containsKey(c);
    }

    /**
     * Returns a factory instance for the given class. The factory is independent
     * from all previously generated instances.
     *
     * @param c the class
     * @return the object factory.
     */
    public GenericObjectFactory getFactoryForClass(final Class c) {
        final GenericObjectFactory factory = (GenericObjectFactory) this.objectMappings.get(c);
        if (factory == null) {
            return null;
        }
        return factory.getInstance();
    }

    /**
     * Returns the manual mapping definition for the given class, or null, if
     * not manual definition exists.
     *
     * @param c the class for which to check the existence of the definition
     * @return the manual mapping definition or null.
     */
    public ManualMappingDefinition getManualMappingDefinition(final Class c) {
        return (ManualMappingDefinition) this.manualMappings.get(c);
    }

    /**
     * Returns the multiplex definition for the given class, or null, if no
     * such definition exists.
     *
     * @param c the class for which to check the existence of the multiplexer
     * @return the multiplexer for the class, or null if no multiplexer exists.
     */
    public MultiplexMappingDefinition getMultiplexDefinition(final Class c) {
        final MultiplexMappingDefinition definition = (MultiplexMappingDefinition)
        this.multiplexMappings.get(c);
        return definition;
    }

}

⌨️ 快捷键说明

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