首页

关于infoset-xproc源码包library组件库、不同组件注入方式护IComponentRegistry源码说明

标签:infoset-xproc,library组件库,componentRegistry,加载解析xml,组件     发布时间:2018-07-06   

一、前言

关于infoset-xproc源码包org.infoset.component.library、 org.infoset.component.ComponentRegistry、 org.infoset.component.ComponentDefinition组件库类,通过registry.xml文件加载配置的注册加载组件,详情参见源码说明部分。

二、源码说明

1.Library类

package org.infoset.component.library;@b@@b@import java.net.URI;@b@import java.net.URL;@b@import org.infoset.component.ComponentRegistry;@b@import org.infoset.component.ComponentRegistryReader;@b@import org.infoset.xml.XMLException;@b@@b@public class Library@b@{@b@  static final Library instance = new Library();@b@  public static final URI REGISTRY_DOCUMENT = URI.create(instance.getClass().getResource("registry.xml").toString());@b@  ComponentRegistry registry = null;@b@@b@  public static ComponentRegistry getRegistry()@b@    throws XMLException@b@  {@b@    if (instance.registry == null)@b@      synchronized (instance) {@b@        instance.registry = ComponentRegistryReader.getInstance().read(REGISTRY_DOCUMENT);@b@      }@b@@b@    return instance.registry;@b@  }@b@}

2.registry.xml

<?xml version="1.0" encoding="UTF-8"?>@b@<component-registry name="xproc-library" xmlns="http://www.infoset.org/Vocabulary/XProc/ComponentRegistry/2007/1/0"@b@                    xmlns:p="http://www.w3.org/2007/03/xproc"@b@                    vendor="infoset.org"@b@                    location="http://www.infoset.org"@b@                    version="0.7.0">@b@   <component name="p:identity" class="org.infoset.component.library.IdentityComponentDefinition"/>@b@   <component name="p:xslt" class="org.infoset.component.library.XSLTComponentDefinition"/>   @b@   <component name="p:insert" class="org.infoset.component.library.InsertComponentDefinition"/>   @b@   <component name="p:replace" class="org.infoset.component.library.ReplaceComponentDefinition"/>   @b@   <component name="p:set-attributes" class="org.infoset.component.library.SetAttributesComponentDefinition"/>   @b@   <component name="p:wrap" class="org.infoset.component.library.WrapComponentDefinition"/>   @b@   <component name="p:validate" class="org.infoset.component.library.XMLSchemaComponentDefinition"/>   @b@   <component name="p:xinclude" class="org.infoset.component.library.XIncludeComponentDefinition"/>   @b@   <component name="p:rename" class="org.infoset.component.library.RenameComponentDefinition"/>   @b@   <component name="p:url" class="org.infoset.component.library.URLComponentDefinition"/>   @b@   <component name="p:parse" class="org.infoset.component.library.ParseComponentDefinition"/>   @b@   <component name="p:serialize" class="org.infoset.component.library.SerializeComponentDefinition"/>   @b@</component-registry>

3.ComponentRegistry接口

package org.infoset.component;@b@@b@import java.util.List;@b@import java.util.Map;@b@import org.infoset.xml.Name;@b@@b@public abstract interface ComponentRegistry extends Map<Name, ComponentDefinition>@b@{@b@  public abstract Name getName();@b@@b@  public abstract List<ComponentRegistry> getSubordinate();@b@}

4.ComponentDefinition类

package org.infoset.component;@b@@b@import java.util.List;@b@import java.util.Map;@b@import org.infoset.xml.Name;@b@@b@public abstract interface ComponentRegistry extends Map<Name, ComponentDefinition>@b@{@b@  public abstract Name getName();@b@@b@  public abstract List<ComponentRegistry> getSubordinate();@b@}

5.IdentityComponentDefinition类

package org.infoset.component.library;@b@@b@import java.net.URI;@b@import org.infoset.component.ItemFilterComponentDefinition;@b@import org.infoset.xml.Document;@b@import org.infoset.xml.Item;@b@import org.infoset.xml.ItemDestination;@b@import org.infoset.xml.Name;@b@import org.infoset.xml.XMLException;@b@import org.infoset.xml.filter.ItemFilter;@b@@b@public class IdentityComponentDefinition extends ItemFilterComponentDefinition@b@{@b@  Document staticInput = null;@b@  URI staticLocation = null;@b@@b@  public IdentityComponentDefinition(Name name, String vendor, String version, URI vendorLocation)@b@  {@b@    super(name, vendor, version, vendorLocation, Identity.class);@b@  }@b@@b@  public static class Identity@b@    implements ItemFilter@b@  {@b@    ItemDestination output;@b@@b@    public Identity()@b@    {@b@      this.output = null;@b@    }@b@@b@    public void send(Item item) throws XMLException {@b@      if (this.output != null)@b@        this.output.send(item);@b@    }@b@@b@    public void attach(ItemDestination dest) {@b@      this.output = dest;@b@    }@b@  }@b@}
package org.infoset.component;@b@@b@import java.net.URI;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.List;@b@import org.infoset.xml.Name;@b@import org.infoset.xml.XMLException;@b@import org.infoset.xml.filter.ItemFilter;@b@@b@public class ItemFilterComponentDefinition extends ComponentDefinitionBase@b@{@b@  static Name DOCUMENT_INPUT_PORT = Name.create("source");@b@  static Name RESULT_OUTPUT_PORT = Name.create("result");@b@  static List<Name> inputs = new ArrayList();@b@  static List<Name> outputs = new ArrayList();@b@  Class filterClass;@b@@b@  public ItemFilterComponentDefinition(Name name, String vendor, String version, URI vendorLocation, Class filterClass)@b@  {@b@    super(name, vendor, version, vendorLocation);@b@    this.filterClass = filterClass;@b@  }@b@@b@  public Component newInstance() throws XMLException@b@  {@b@    ItemFilter filter;@b@    try {@b@      filter = (ItemFilter)this.filterClass.newInstance();@b@      return new ItemFilterComponent(this.name, this.vendor, this.version, filter);@b@    } catch (IllegalAccessException ex) {@b@      throw new XMLException("Cannot instantiate filter instance.", ex);@b@    } catch (InstantiationException ex) {@b@      throw new XMLException("Cannot instantiate filter instance.", ex);@b@    }@b@  }@b@@b@  public Collection<Name> getInputs() {@b@    return inputs;@b@  }@b@@b@  public Collection<Name> getOutputs() {@b@    return outputs;@b@  }@b@@b@  static@b@  {@b@    inputs.add(DOCUMENT_INPUT_PORT);@b@    outputs.add(RESULT_OUTPUT_PORT);@b@  }@b@}
package org.infoset.component;@b@@b@import java.net.URI;@b@import java.util.Collection;@b@import org.infoset.xml.Name;@b@import org.infoset.xml.XMLException;@b@@b@public abstract class ComponentDefinitionBase@b@  implements ComponentDefinition@b@{@b@  protected Name name;@b@  protected String vendor;@b@  protected String version;@b@  protected URI vendorLocation;@b@@b@  public ComponentDefinitionBase(Name name, String vendor, String version, URI vendorLocation)@b@  {@b@    this.name = name;@b@    this.vendor = vendor;@b@    this.version = version;@b@    this.vendorLocation = vendorLocation;@b@  }@b@@b@  public abstract Component newInstance() throws XMLException;@b@@b@  public Name getName()@b@  {@b@    return this.name;@b@  }@b@@b@  public String getVersion() {@b@    return this.version;@b@  }@b@@b@  public String getVendor() {@b@    return this.vendor;@b@  }@b@@b@  public URI getVendorLocation() {@b@    return this.vendorLocation;@b@  }@b@@b@  public abstract Collection<Name> getInputs();@b@@b@  public abstract Collection<Name> getOutputs();@b@}

6.组件Component接口及ComponentBase实现类

package org.infoset.component;@b@@b@import java.net.URI;@b@import java.util.Collection;@b@import java.util.logging.Logger;@b@import org.infoset.xml.Document;@b@import org.infoset.xml.DocumentLoader;@b@import org.infoset.xml.Infoset;@b@import org.infoset.xml.ItemDestination;@b@import org.infoset.xml.Name;@b@import org.infoset.xml.NamespaceScope;@b@import org.infoset.xml.XMLException;@b@@b@public abstract interface Component@b@{@b@  public abstract void init(Context paramContext)@b@    throws XMLException;@b@@b@  public abstract void bindResource(Name paramName, Document paramDocument)@b@    throws XMLException;@b@@b@  public abstract void bindResource(Name paramName, URI paramURI)@b@    throws XMLException;@b@@b@  public abstract void bindOutputPort(Name paramName, ItemDestination paramItemDestination)@b@    throws XMLException;@b@@b@  public abstract void onStart()@b@    throws XMLException;@b@@b@  public abstract ItemDestination getInputPort(Name paramName)@b@    throws XMLException;@b@@b@  public abstract void onDocumentInput(Name paramName, Document paramDocument)@b@    throws XMLException;@b@@b@  public abstract void onEnd()@b@    throws XMLException;@b@@b@  public static abstract interface Context@b@  {@b@    public abstract Logger getLog();@b@@b@    public abstract NamespaceScope getNamespaceScope();@b@@b@    public abstract Object getParameter(Name paramName);@b@@b@    public abstract Collection<Name> getParameterNames();@b@@b@    public abstract URI getBaseURI();@b@@b@    public abstract DocumentLoader getDocumentLoader();@b@@b@    public abstract Infoset getInfoset();@b@  }@b@}
package org.infoset.component;@b@@b@import java.net.URI;@b@import org.infoset.xml.Document;@b@import org.infoset.xml.ItemDestination;@b@import org.infoset.xml.Name;@b@import org.infoset.xml.XMLException;@b@@b@public class ComponentBase@b@  implements Component@b@{@b@  protected Name name;@b@  protected String vendor;@b@  protected String version;@b@  protected Component.Context context;@b@@b@  protected ComponentBase(Name name)@b@  {@b@    this(name, null, null);@b@  }@b@@b@  protected ComponentBase(Name name, String vendor, String version)@b@  {@b@    this.name = name;@b@    this.vendor = vendor;@b@    this.version = version;@b@  }@b@@b@  public void init(Component.Context context)@b@    throws XMLException@b@  {@b@    this.context = context;@b@  }@b@@b@  public void bindResource(Name port, Document document)@b@    throws XMLException@b@  {@b@    throw new XMLException("Input port " + port + " unknown and cannot be bound as a resource.");@b@  }@b@@b@  public void bindResource(Name port, URI location)@b@    throws XMLException@b@  {@b@    throw new XMLException("Input port " + port + " unknown and cannot be bound as a resource.");@b@  }@b@@b@  public void bindOutputPort(Name port, ItemDestination output)@b@    throws XMLException@b@  {@b@    throw new XMLException("Output port " + port + " unknown.");@b@  }@b@@b@  public void onStart()@b@    throws XMLException@b@  {@b@  }@b@@b@  public ItemDestination getInputPort(Name port)@b@    throws XMLException@b@  {@b@    throw new XMLException("Input port " + port + " unknown.");@b@  }@b@@b@  public void onDocumentInput(Name port, Document doc)@b@    throws XMLException@b@  {@b@    throw new XMLException("Input port " + port + " unknown.");@b@  }@b@@b@  public void onEnd()@b@    throws XMLException@b@  {@b@  }@b@}