advertisementfactory.java

来自「JXTA&#8482 is a set of open, generalize」· Java 代码 · 共 402 行 · 第 1/2 页

JAVA
402
字号
        // our key is the doctype names.        return java.lang.String.class;    }        /**     *  {@inheritDoc}     */    @Override    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 (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {                LOG.log(Level.FINE, "Failed to register \'" + className + "\'", all);            }        }                return registeredSomething;    }        /**     *  Register an instantiator for and advertisement type to allow instances     *  of that type to be created.     *     *  @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 a new instance of {@link Advertisement} matching the type     * specified by the {@code advertisementType} parameter.     *     * @param advertisementType Specifies the type of advertisement to create.     * @return The instance of {@link Advertisement}.     * @throws NoSuchElementException if there is no matching advertisement type.     */    public static Advertisement newAdvertisement(String advertisementType) {        factory.loadProviders();                Instantiator instantiator = factory.getInstantiator(advertisementType);                Advertisement a = instantiator.newInstance();                return a;    }        /**     * Constructs an instance of {@link Advertisement} from the provided     * <code>InputStream</code>. The content type of the stream is declared via     * the <code>mimetype</code> parameter.     *     * @deprecated Please convert your code to construct an {@code XMLDocument}     * using {@code StructuredDocumentFactory} and then call      * {@link AdvertisementFactory#newAdvertisement(XMLElement)}. For example :     * <p/><pre>     *   XMLDocument xml = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( MimeMediaType.XMLUTF8, is );     * </pre>     * <b>or frequently:</b>     * <p/><pre>     *   XMLDocument xml = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( msgElement );     * </pre>     * <b>followed by:</b>     * <p/><pre>     *   Advertisement adv = AdvertisementFactory.newAdvertisement(xml);     * </pre>     *     * @param mimetype Specifies the mime media type of the stream being read.     * @param stream input 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.     */    @Deprecated    public static Advertisement newAdvertisement(MimeMediaType mimetype, InputStream stream) throws IOException {        StructuredDocument doc = StructuredDocumentFactory.newStructuredDocument(mimetype, stream);                if (!(doc instanceof XMLDocument)) {            throw new IllegalArgumentException("Advertisements must be XML");        }                return newAdvertisement((XMLDocument) doc);    }        /**     * Reconstructs an instance of {@link Advertisement} from the provided     * <code>Reader</code>. The content type of the reader is declared via the     * <code>mimetype</code> parameter.     *     * @deprecated Please convert your code to construct an {@code XMLDocument}     * using {@code StructuredDocumentFactory} and then call      * {@link AdvertisementFactory#newAdvertisement(XMLElement)}. For example :     * <p/><pre>     *   XMLDocument xml = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( MimeMediaType.XMLUTF8, reader );     * </pre>     * <b>or frequently:</b>     * <p/><pre>     *   XMLDocument xml = (XMLDocument) StructuredDocumentFactory.newStructuredDocument( msgElement );     * </pre>     * <b>followed by:</b>     * <p/><pre>     *   Advertisement adv = AdvertisementFactory.newAdvertisement(xml);     * </pre>     *     * @param mimetype Specifies the mime media type of the stream being read.     * @param source 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.     * @throws UnsupportedOperationException if the specified mime type is not     *  associated with a text oriented document type.     */    @Deprecated    public static Advertisement newAdvertisement(MimeMediaType mimetype, Reader source) throws IOException {        StructuredTextDocument doc = (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument(mimetype, source);                return newAdvertisement(doc);    }        /**     * Reconstructs an instance of {@link Advertisement} matching the type     * specified by the {@code root} parameter.     *     * @deprecated Advertisements must be encoded in XML. This is a legacy     * static constructor. You should convert your code to use the      * {@link AdvertisementFactory#newAdvertisement(XMLElement) XMLElement}     * version.     *     * @param root Specifies a portion of a StructuredDocument which will be     * converted into an Advertisement.     * @return The instance of {@link Advertisement}.     * @throws NoSuchElementException if there is no advertisement type     * matching the type of the root node.     */    @Deprecated    public static Advertisement newAdvertisement(TextElement root) {        if (!(root instanceof XMLElement)) {            throw new IllegalArgumentException("Advertisements must be XML");        }                return newAdvertisement((XMLElement) root);    }    /**     * Reconstructs an instance of {@link Advertisement} matching the type     * specified by the {@code root} parameter.     *     * @param root Specifies a portion of an XMLElement which will be     * converted into an Advertisement.     * @return The instance of {@link Advertisement}.     * @throws NoSuchElementException if there is no advertisement type     * matching the type of the root node.     */    public static Advertisement newAdvertisement(XMLElement root) {        factory.loadProviders();                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.        Attribute type = root.getAttribute("type");        if (null != type) {            try {                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 = factory.getInstantiator(root.getName());        }                Advertisement a = instantiator.newInstance(root);                return a;    }}

⌨️ 快捷键说明

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