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

📄 extensionpoint.java

📁 google的gdata api包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    return manifest;  }  @Override  protected void generate(XmlWriter w, ExtensionProfile p,      XmlWriter.Namespace namespace, String localName,      List<XmlWriter.Attribute> attrs, AttributeGenerator generator)      throws IOException {    // validate    if (generator.getContent() != null) {      throw new IllegalStateException(          "No content allowed on an extension point");    }    try {      ExtensionManifest manifest = p.getManifest(this.getClass());      if (manifest != null) {        checkRequiredExtensions(manifest);      }    } catch (ParseException e) {      throw new IllegalStateException(e.getMessage());    }    // generate XML    generateStartElement(w, namespace, localName, attrs, null);    generateExtensions(w, p);    w.endElement(namespace, localName);  }  @Override  public XmlParser.ElementHandler getHandler(ExtensionProfile p,      String namespace, String localName, Attributes attrs)      throws ParseException, IOException {    return new ExtensionHandler(p, this.getClass(), attrs);  } /**   * Generates XML corresponding to the type implementing {@link   * ExtensionPoint}. The reason this routine is necessary is that the   * embedded XML blob may contain namespace declarations.   */  protected void generateStartElement(XmlWriter w,                                      XmlWriter.Namespace namespace,                                      String elementName,                                      Collection<XmlWriter.Attribute>                                       additionalAttrs,                                      Collection<XmlWriter.Namespace>                                       additionalNs)      throws IOException {    XmlBlob.startElement(w, namespace, elementName, xmlBlob,                         additionalAttrs, additionalNs);  }  /**   * Generates XML corresponding to extended properties. Implementations in   * extended classes should always call the base class to allow for nested   * extensions.   *   * @param   w   *            Output writer.   *   * @param   extProfile   *            Extension profile for use by nested extensions.   *   * @throws  IOException   */  protected void generateExtensions(XmlWriter w,                                    ExtensionProfile extProfile)      throws IOException {    for (Extension ext: nonRepeatingExtensionMap.values()) {      ext.generate(w, extProfile);    }    for (List<Extension> extList: repeatingExtensionMap.values()) {      w.startRepeatingElement();      for (Extension ext: extList) {        ext.generate(w, extProfile);      }      w.endRepeatingElement();    }    if (xmlBlob != null) {      w.innerXml(xmlBlob.getBlob());    }  }  /**   * Initializes parser handler's XML blob state.   * Should be called by the handler's constructor in order to honor   * {@link ExtensionProfile#declareArbitraryXmlExtension(Class)}.   */  protected void initializeArbitraryXml(ExtensionProfile profile,                                        Class extPoint,                                        ElementHandler handler)      throws IOException {    ExtensionManifest manifest = getManifest(profile, extPoint);    if (manifest != null && manifest.arbitraryXml) {      handler.initializeXmlBlob(xmlBlob,                                /* mixedContent */ false,                                /* fullTextIndex */ false);    }  }  /**   * XML parser callback for extended properties. Implementations in   * extended classes should always call the base class to allow for   * nested extensions.   *   * @param   extProfile   *            Extension profile for use by nested element handlers.   *   * @param   extPoint   *            Current active ExtensionPoint class within which you're   *            looking for a handler for a nested extension element.   *   * @param   namespaceUri   *            Namespace URI of the XML element.   *   * @param   localName   *            Name of the XML element.   *   * @param   attrs   *            Child element attributes. These attributes will be   *            communicated to the returned {@link ElementHandler}   *            through its {@link   *            ElementHandler#processAttribute(String, String,   *            String)} method. They are passed here because sometimes   *            the value of some attribute determines the element's   *            content type, so different element handlers may be   *            needed.   *   * @return  Element handler for the custom tag or {@code null} if the tag   *          is not recognized. Unrecognized tags are stored in the XML blob.   *   * @throws  ParseException   *            XML schema error. Could be a result of having a   *            duplicate entry, illegal contents (such as unrecognized   *            attributes or nested elements), etc.   */  protected ElementHandler getExtensionHandler(ExtensionProfile extProfile,                                               Class extPoint,                                               String namespaceUri,                                               String localName,                                               Attributes attrs)      throws ParseException, IOException {    ExtensionManifest manifest = getManifest(extProfile, extPoint);    if (manifest == null) {      return null;    }    // Look for an explicit match, followed by a wildcarded namespace match.    ExtensionDescription extDescription =      manifest.supportedExtensions.get(new Pair(namespaceUri, localName));    if (extDescription == null) {      extDescription =        manifest.supportedExtensions.get(new Pair(namespaceUri, "*"));      if (extDescription == null) {        // extension point) could just live here.        return null;      }    }    Class<? extends Extension> extClass = extDescription.getExtensionClass();    if (extClass == null) {      return null;    }    Extension extension = null;    // If an aggregate extension type, retrieve existing instance (if any)    if (extDescription.isAggregate()) {      extension = getExtension(extClass);    }    boolean needsAdd = true;    if (extension == null) {      // Create an extension instance.      try {        extension = extClass.newInstance();      } catch (InstantiationException e) {        throw new ParseException("Unable to create extension", e);      } catch (IllegalAccessException e) {        throw new ParseException("Unable to create extension", e);      }    } else {      needsAdd = false;    }    // Retrieve the handler.    ElementHandler handler = extension.getHandler(extProfile, namespaceUri,                                                  localName, attrs);    // Store the new extension instance.    if (needsAdd) {      if (extDescription.isRepeatable()) {        addRepeatingExtension(extension, extClass);      } else {        boolean added = addExtension(extension, extClass);        if (!added) {          throw new ParseException(            "Duplicate extension element " + namespaceUri + ":" + localName);        }      }    }    return handler;  }  /** Checks whether all required extensions are present. */  protected void checkRequiredExtensions(ExtensionManifest manifest)      throws ParseException {    for (ExtensionDescription extDescription:         manifest.supportedExtensions.values()) {      if (extDescription.isRequired()) {        Class extClass = extDescription.getExtensionClass();        boolean found = (extDescription.isRepeatable()                         ? repeatingExtensionMap.containsKey(extClass)                         : nonRepeatingExtensionMap.containsKey(extClass));        if (!found) {          throw new ParseException(            "Required extension element " +            extDescription.getNamespace().getUri() + ":" +            extDescription.getLocalName() + " not found.");        }      }    }  }  /**   * ElementHandler implementation for handlers associated with an   * ExtensionPoint class. Provides common initialization and   * code for looking up handlers defined within the ExtensionProfile   * associated with the ExtensionPoint.   */  public class ExtensionHandler extends AbstractExtension.AttributesHandler {    protected ExtensionProfile extProfile;    protected Class extendedClass;    protected boolean hasExtensions;    protected ExtensionManifest manifest;    /**     * Constructs a new Handler instance that process extensions on a class     * associated with the ExtensionPoint.     *     * @param profile       The extension profile associatd with the Handler.     * @param extendedClass The extended class within the profile for this     *                      handler     */    public ExtensionHandler(ExtensionProfile profile, Class extendedClass)        throws IOException {      this(profile, extendedClass, null);    }    /**     * Constructs a new Handler instance that process extensions on a class     * associated with the ExtensionPoint, and consumes the attributes.     *     * @param profile       The extension profile associatd with the Handler.     * @param extendedClass The extended class within the profile for this     *                      handler     * @param attrs         XML attributes or <code>null</code> to suppress the     *                      use of {@link AttributeHelper}     */    public ExtensionHandler(ExtensionProfile profile, Class extendedClass,        Attributes attrs)        throws IOException {      super(attrs);      this.extProfile = profile;      this.extendedClass = extendedClass;      // If the extended class is defined within the profile, then enable      // extension handling and arbitrary XML handling.      this.manifest = profile.getManifest(extendedClass);      if (this.manifest != null) {        hasExtensions = true;        if (manifest.arbitraryXml) {          initializeArbitraryXml(extProfile, extendedClass, this);        }      }    }    public XmlParser.ElementHandler getChildHandler(String namespace,                                                    String localName,                                                    Attributes attrs)        throws ParseException, IOException {      // If extensions have been defined for the extended class, then      // look for a handler.      if (hasExtensions) {        XmlParser.ElementHandler extensionHandler =          getExtensionHandler(extProfile, extendedClass,                              namespace, localName, attrs);        if (extensionHandler != null) {          return extensionHandler;        }      }      return super.getChildHandler(namespace, localName, attrs);    }    public void processEndElement() throws ParseException {      super.processEndElement();      if (this.manifest != null) {        checkRequiredExtensions(this.manifest);      }      //      // Iterate through all contained Extension instances and enable them      // to validate against the full ExtensionPoint state (including      // sibling Extension instances).      //      for (Extension extension :  nonRepeatingExtensionMap.values()) {        if (extension instanceof ValidatingExtension) {          ((ValidatingExtension) extension).validate(ExtensionPoint.this);        }      }      for (List<Extension> extList : repeatingExtensionMap.values()) {        for (Extension extension : extList) {          if (extension instanceof ValidatingExtension) {            ((ValidatingExtension) extension).validate(ExtensionPoint.this);          }        }      }    }  }}

⌨️ 快捷键说明

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