001    /*
002     * Created on Apr 3, 2009
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005     * in compliance with the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License
010     * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011     * or implied. See the License for the specific language governing permissions and limitations under
012     * the License.
013     *
014     * Copyright @2009 the original author or authors.
015     */
016    package org.fest.swing.junit.xml;
017    
018    import javax.xml.parsers.DocumentBuilderFactory;
019    import javax.xml.parsers.ParserConfigurationException;
020    
021    import org.w3c.dom.Document;
022    
023    /**
024     * Understands a DOM-based XML document. This class is intended for internal use only. It is just a thin wrapper around
025     * a DOM <code>{@link Document}</code>. It only provides the necessary functionality needed by the FEST-Swing JUnit
026     * extension.
027     *
028     * @author Alex Ruiz
029     */
030    public class XmlDocument {
031    
032      private final Document document;
033    
034      /**
035       * Creates a new </code>{@link XmlDocument}</code>.
036       * @throws ExceptionInInitializerError if the XML document could not be created.
037       */
038      public XmlDocument() {
039        try {
040          document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
041        } catch (ParserConfigurationException e) {
042          throw new ExceptionInInitializerError(e);
043        }
044      }
045    
046      /**
047       * Creates and adds a new XML root node.
048       * @param name the name of the XML node to create.
049       * @return the created root node.
050       */
051      public XmlNode newRoot(String name) {
052        XmlNode root = new XmlNode(document.createElement(name));
053        document.appendChild(root.target());
054        return root;
055      }
056    }