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

📄 advertisementfactory.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *
     *  @since JXTA 1.0
     *
     *  @return Class object of the key type.
     **/
    public Class getClassForKey() {
        // our key is the doctype names.
        return java.lang.String.class;
    }

    /**
     *  Register a class with the factory from its class name. We override the
     *  standard implementation to get the advertisement type from the class and
     *  use that as the key to register the class with the factory.
     *
     *  @since JXTA 1.0
     *
     * @param className The class name which will be regiestered.
     * @return boolean true if the class was registered otherwise false.
     **/
    protected boolean registerAssoc( String className ) {
        boolean registeredSomething = false;

        try {
            Class advClass = Class.forName( className + "$Instantiator" );

            Instantiator instantiator = (Instantiator) advClass.newInstance();

            String advType = instantiator.getAdvertisementType( );

            registeredSomething = registerAdvertisementInstance( advType, instantiator );
        } catch( Exception all ) {
            if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Failed to register '" + className + "'", all );
        }

        return registeredSomething;
    }

    /**
     *  Register an instantiator for and advertisement type to allow instances
     *  of that type to be created.
     *
     *  @since JXTA 1.0
     *
     *  @param rootType  the identifying value for this advertisement instance
     *   type
     *  @param instantiator   the instantiator to use in constructing objects
     *   of this rootType.
     *  @return boolean  true if the rootType type is registered. If there is
     *   already a constructor for this type then false will be returned.
     **/
    public static boolean registerAdvertisementInstance( String rootType, Instantiator instantiator ) {
        boolean result = factory.registerAssoc( rootType, instantiator );

        return result;
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>advertisementType</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param advertisementType Specifies the type of advertisement to create.
     * @return The instance of {@link Advertisement} or null if it
     * could not be created.
     **/
    public static Advertisement newAdvertisement( String advertisementType ) {
        return newAdvertisement( advertisementType, -1L );
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>advertisementType</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param advertisementType Specifies the type of advertisement to create.
     * @param timeout  positive number of milliseconds until this advertisement
     *  will expire. Values less than 1 are interpreted as requests for infinite
     *  timeout
     * @return The instance of {@link Advertisement} or null if it could not be
     *  created.
     **/
    public static Advertisement newAdvertisement( String advertisementType, long timeout ) {
        if( ! factory.loadedProperty )
            factory.loadedProperty = factory.doLoadProperty();

        Instantiator instantiator =
        (Instantiator) factory.getInstantiator( advertisementType );

        Advertisement a = instantiator.newInstance( );

        a.setExpiration(timeout);

        return a;
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>advertisementType</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param mimetype Specifies the mime media type of the stream being read.
     * @param stream imput stream used to read data to construct the advertisement
     * @return The instance of {@link Advertisement}
     * @throws IOException error reading message from input stream
     * @throws NoSuchElementException if there is no matching advertisement type
     * for the type of document read in.
     **/
    public static Advertisement newAdvertisement( MimeMediaType mimetype, InputStream stream )
    throws IOException {
        return newAdvertisement( mimetype, stream, -1 );
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>advertisementType</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param mimetype Specifies the mime media type to be associated with the
     * {@link StructuredDocument}> to be created.
     * @param stream imput stream used to read data to construct the advertisement
     * @param timeout  positive number of milliseconds until this advertisement
     *  will expire. Values less than 1 are interpreted as requests for infinite
     *  timeout
     * @return The instance of {@link Advertisement} or null if it
     * could not be created.
     *
     * @exception IOException error reading message from input stream
     * @throws NoSuchElementException if there is no matching advertisement type
     * for the type of document read in.
     **/
    public static Advertisement newAdvertisement( MimeMediaType mimetype, InputStream stream, long timeout )
    throws IOException {

        StructuredTextDocument doc = (StructuredTextDocument)
        StructuredDocumentFactory.newStructuredDocument( mimetype, stream );

        return newAdvertisement( doc, timeout );
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>root</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param root Specifies a portion of a StructuredDocument which will be
     * converted into an Advertisement.
     * @return The instance of {@link Advertisement} or null if it
     * could not be created.
     * @throws NoSuchElementException if there is no matching advertisement type
     * for the type of document read in.
     **/
    public static Advertisement newAdvertisement( TextElement root ) {

        return newAdvertisement(root, -1 );
    }

    /**
     * Constructs an instance of {@link Advertisement} matching the type
     * specified by the <CODE>root</CODE> parameter.
     *
     * @since JXTA 1.0
     *
     * @param root Specifies a portion of a StructuredDocument which will be
     *  converted into an Advertisement.
     * @param timeout  Positive number of milliseconds until this advertisement
     *  will expire. Values less than 1 are interpreted as requests for infinite
     *  timeout.
     * @return The instance of {@link Advertisement} or null if it
     *  could not be created.
     * @throws NoSuchElementException if there is no matching advertisement type
     * for the type of document read in.
     **/
    public static Advertisement newAdvertisement( TextElement root, long timeout ) {
        if( ! factory.loadedProperty )
            factory.loadedProperty = factory.doLoadProperty();

        Instantiator instantiator = null;

        // The base type of the advertisement may be overridden by a type
        // declaration. If this is the case, then we try to use that as the
        // key rather than the root name.
        if( root instanceof Attributable ) {
            Attribute type = ((Attributable)root).getAttribute( "type" );

            if( null != type ) {
                try {
                    instantiator =
                    (Instantiator) factory.getInstantiator( type.getValue() );
                } catch( NoSuchElementException notThere ) {
                    // do nothing, its not fatal
                }
            }
        }

        // Don't have an instantiator for the type attribute, try the root name
        if( null == instantiator )
        {
            instantiator =
            (Instantiator) factory.getInstantiator( root.getName() );
        }

        Advertisement a = instantiator.newInstance( root );

        a.setExpiration(timeout);

        return a;
    }
}

⌨️ 快捷键说明

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