import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/******************************************************************
 * This class reproduces a suspected bug triggered when you try to 
 * import a doc frag that contains elements with attributes. Doing
 * so results in a HIERARCHY_REQUEST_ERR.
 * 
 ******************************************************************
 */
class DOMImportTest 
  {
  public static void main(String[] args) 
	  {
		try
      {
      DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder         builder = factory.newDocumentBuilder();

      Document  srcDoc  = builder.parse("test.xml");
      
      NodeList  nlist = null;
      
      // Create a doc frag containing of the contents of the <DF1> element
      DocumentFragment  df1   = srcDoc.createDocumentFragment();
      nlist = srcDoc.getElementsByTagName("DF1");
      if (nlist.getLength() > 0)
        {
        NodeList children = nlist.item(0).getChildNodes();
        int ncount = children.getLength();
        for (int i = 0; i < ncount; i++)
          {
          df1.appendChild(children.item(i).cloneNode(true));
          }
        }
      else
        throw new Exception("No <DF1> tag in test file!");
      
      // Create a doc frag containing of the contents of the <DF2> element.
      DocumentFragment  df2   = srcDoc.createDocumentFragment();
      nlist = srcDoc.getElementsByTagName("DF2");
      if (nlist.getLength() > 0)
        {
        NodeList children = nlist.item(0).getChildNodes();
        int ncount = children.getLength();
        for (int i = 0; i < ncount; i++)
          {
          df2.appendChild(children.item(i).cloneNode(true));
          }
        }
      else
        throw new Exception("No <DF2> tag in test file!");
      
      // Get the <NON_DF> element
      Node nonDF = null;
      nlist = srcDoc.getElementsByTagName("NON_DF");
      if (nlist.getLength() > 0)
        {
        nonDF = nlist.item(0);
        }
      else
        throw new Exception("No <NON_DF> tag in test file!");

      // Begin the target doc
      Document tgtDoc = builder.newDocument();
      Node     root   = tgtDoc.appendChild(tgtDoc.createElement("ROOT"));
      
      // Try to import the DF1 doc frag and insert it if successful.
      try
        {
        root.appendChild(tgtDoc.importNode(df1, /*deep=*/true));
        System.out.println("Imported and inserted <DF1> doc frag successfully!");
        }
      catch (Exception x)
        {
        x.printStackTrace(System.out);
        System.out.println("FAILED to import and/or insert <DF1> doc frag!");
        }

      // Try to import the DF2 doc frag and insert it if successful.
      try
        {
        root.appendChild(tgtDoc.importNode(df2, /*deep=*/true));
        System.out.println("Imported and inserted <DF2> doc frag successfully!");
        }
      catch (Exception x)
        {
        x.printStackTrace(System.out);
        System.out.println("FAILED to import and/or insert <DF2> doc frag!");
        }

      // Try the same thing for the <NON_DF> node to show that it's isolated to doc frags
      try
        {
        root.appendChild(tgtDoc.importNode(nonDF, /*deep=*/true));
        System.out.println("Imported and inserted NON-doc frag successfully!");
        }
      catch (Exception x)
        {
        x.printStackTrace(System.out);
        System.out.println("FAILED to import and/or insert NON-doc frag!");
        }

      }
    catch (Exception x)
      {
      x.printStackTrace(System.out);
      }
    
    }
  }
