maven2-plugins/myfaces-builder-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/builder/trinidad/util/XIncludeFilter.java [40:184]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class XIncludeFilter extends XMLFilterImpl
{
  public final static String XINCLUDE_NAMESPACE = "http://www.w3.org/2001/XInclude";

  public XIncludeFilter(
    XMLReader parent,
    URL       base)
  {
    super(parent);
    bases.push(base);
  }

  public void setDocumentLocator(
    Locator locator)
  {
    locators.push(locator);
    super.setDocumentLocator(locator);
  }

  public void startPrefixMapping(
    String prefix,
    String namespaceURI) throws SAXException
  {
    if (!namespaceURI.equals(XINCLUDE_NAMESPACE))
    {
      namespaces.declarePrefix(prefix, namespaceURI);
      super.startPrefixMapping(prefix, namespaceURI);
    }
  }

  public void startElement(
    String     namespaceURI,
    String     localName,
    String     qualifiedName,
    Attributes attributes) throws SAXException
  {
    namespaces.pushContext();
    // support both DTD and Schema
    if (namespaceURI.equals(XINCLUDE_NAMESPACE) &&
        localName.equals("include"))
    {
      // href attribute is required
      String href = attributes.getValue("href");
      if (href == null)
        throw new SAXException("Missing href attribute");

      // xpointer attribute is optional
      String xpointer = attributes.getValue("xpointer");

      doXInclude(href, xpointer);
    }
    else
    {
      super.startElement(namespaceURI, localName, qualifiedName, attributes);
    }
  }

  public void endElement(
    String     namespaceURI,
    String     localName,
    String     qualifiedName) throws SAXException
  {
    if (!namespaceURI.equals(XINCLUDE_NAMESPACE))
      super.endElement(namespaceURI, localName, qualifiedName);

    namespaces.popContext();
  }

  public void startDocument() throws SAXException
  {
    if (depth == 0)
      super.startDocument();

    depth++;
  }

  public void endDocument() throws SAXException
  {
    locators.pop();
    depth--;

    if (depth == 0)
      super.endDocument();
  }

  private void doXInclude(
    String href,
    String xpointer) throws SAXException
  {
    URL target = resolveURL(href);

    try
    {
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();
      SAXParser saxParser = saxFactory.newSAXParser();
      XMLReader reader = saxParser.getXMLReader();
      if (xpointer != null)
      {
        // pass all parsed SAX events through XPointerFilter
        reader = new XPointerFilter(reader, namespaces, xpointer);
      }
      // pass all selected XPointer SAX events to XIncludeFilter
      reader.setContentHandler(this);
      reader.setEntityResolver(this);
      reader.setErrorHandler(this);
      reader.setDTDHandler(this);

      // push on new base URL in case of nested xi:include
      bases.push(target);
      reader.parse(new InputSource(target.openStream()));
      bases.pop();
    }
    catch (ParserConfigurationException e)
    {
      throw new SAXException("Error during xinclude of " +
                             target.toExternalForm(), e);
    }
    catch (IOException e)
    {
      throw new SAXException("Error during xinclude of " +
                             target.toExternalForm(), e);
    }
  }

  private URL resolveURL(
    String href) throws SAXException
  {
    URL base = (URL) bases.peek();

    try
    {
      return new URL(base, href);
    }
    catch (MalformedURLException e)
    {
      throw new SAXException("Error resolving href \"" + href + "\" " +
                             "at base \"" + base + "\"", e);
    }
  }

  private int depth = 0;

  private Stack bases = new Stack();
  private Stack locators = new Stack();
  private NamespaceSupport namespaces = new NamespaceSupport();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



maven2-plugins/myfaces-faces-plugin/src/main/java/org/apache/myfaces/buildtools/maven2/plugin/faces/util/XIncludeFilter.java [40:184]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
public final class XIncludeFilter extends XMLFilterImpl
{
  public final static String XINCLUDE_NAMESPACE = "http://www.w3.org/2001/XInclude";

  public XIncludeFilter(
    XMLReader parent,
    URL       base)
  {
    super(parent);
    bases.push(base);
  }

  public void setDocumentLocator(
    Locator locator)
  {
    locators.push(locator);
    super.setDocumentLocator(locator);
  }

  public void startPrefixMapping(
    String prefix,
    String namespaceURI) throws SAXException
  {
    if (!namespaceURI.equals(XINCLUDE_NAMESPACE))
    {
      namespaces.declarePrefix(prefix, namespaceURI);
      super.startPrefixMapping(prefix, namespaceURI);
    }
  }

  public void startElement(
    String     namespaceURI,
    String     localName,
    String     qualifiedName,
    Attributes attributes) throws SAXException
  {
    namespaces.pushContext();
    // support both DTD and Schema
    if (namespaceURI.equals(XINCLUDE_NAMESPACE) &&
        localName.equals("include"))
    {
      // href attribute is required
      String href = attributes.getValue("href");
      if (href == null)
        throw new SAXException("Missing href attribute");

      // xpointer attribute is optional
      String xpointer = attributes.getValue("xpointer");

      doXInclude(href, xpointer);
    }
    else
    {
      super.startElement(namespaceURI, localName, qualifiedName, attributes);
    }
  }

  public void endElement(
    String     namespaceURI,
    String     localName,
    String     qualifiedName) throws SAXException
  {
    if (!namespaceURI.equals(XINCLUDE_NAMESPACE))
      super.endElement(namespaceURI, localName, qualifiedName);

    namespaces.popContext();
  }

  public void startDocument() throws SAXException
  {
    if (depth == 0)
      super.startDocument();

    depth++;
  }

  public void endDocument() throws SAXException
  {
    locators.pop();
    depth--;

    if (depth == 0)
      super.endDocument();
  }

  private void doXInclude(
    String href,
    String xpointer) throws SAXException
  {
    URL target = resolveURL(href);

    try
    {
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();
      SAXParser saxParser = saxFactory.newSAXParser();
      XMLReader reader = saxParser.getXMLReader();
      if (xpointer != null)
      {
        // pass all parsed SAX events through XPointerFilter
        reader = new XPointerFilter(reader, namespaces, xpointer);
      }
      // pass all selected XPointer SAX events to XIncludeFilter
      reader.setContentHandler(this);
      reader.setEntityResolver(this);
      reader.setErrorHandler(this);
      reader.setDTDHandler(this);

      // push on new base URL in case of nested xi:include
      bases.push(target);
      reader.parse(new InputSource(target.openStream()));
      bases.pop();
    }
    catch (ParserConfigurationException e)
    {
      throw new SAXException("Error during xinclude of " +
                             target.toExternalForm(), e);
    }
    catch (IOException e)
    {
      throw new SAXException("Error during xinclude of " +
                             target.toExternalForm(), e);
    }
  }

  private URL resolveURL(
    String href) throws SAXException
  {
    URL base = (URL) bases.peek();

    try
    {
      return new URL(base, href);
    }
    catch (MalformedURLException e)
    {
      throw new SAXException("Error resolving href \"" + href + "\" " +
                             "at base \"" + base + "\"", e);
    }
  }

  private int depth = 0;

  private Stack bases = new Stack();
  private Stack locators = new Stack();
  private NamespaceSupport namespaces = new NamespaceSupport();
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



