/[svn]/jsampler/trunk/src/org/jsampler/DOMUtils.java
ViewVC logotype

Contents of /jsampler/trunk/src/org/jsampler/DOMUtils.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (show annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 4626 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 /*
2 * JSampler - a java front-end for LinuxSampler
3 *
4 * Copyright (C) 2005-2006 Grigor Iliev <grigor@grigoriliev.com>
5 *
6 * This file is part of JSampler.
7 *
8 * JSampler is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * JSampler is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with JSampler; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22
23 package org.jsampler;
24
25 import java.io.InputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerConfigurationException;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamResult;
39
40 import org.w3c.dom.Document;
41 import org.w3c.dom.NamedNodeMap;
42 import org.w3c.dom.Node;
43
44 import org.xml.sax.SAXException;
45
46
47 /**
48 * A collection of utility methods for DOM.
49 * @author Grigor Iliev
50 */
51 public class DOMUtils {
52
53 /** Forbits instantiation of this class. */
54 private DOMUtils() { }
55
56 /**
57 * Creates an empty document.
58 * @throws RuntimeException if the creation failed.
59 */
60 public static Document
61 createEmptyDocument() {
62 Document doc = null;
63 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
64
65 try {
66 DocumentBuilder builder = factory.newDocumentBuilder();
67 doc = builder.newDocument();
68 } catch(ParserConfigurationException x) {
69 throw new RuntimeException("Failed to create new document!", x);
70 }
71
72 return doc;
73 }
74 /**
75 * Parses the input from the specified input stream and
76 * returns a new document providing the content read from the stream.
77 * @param in Provides the content to be parsed.
78 * @return A new document providing the content read from the stream.
79 * @throws RuntimeException if the parsing failed.
80 */
81 public static Document
82 readObject(InputStream in) {
83 Document doc = null;
84 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85
86 try {
87 DocumentBuilder builder = factory.newDocumentBuilder();
88 doc = builder.parse(in);
89 } catch(ParserConfigurationException x) {
90 throw new RuntimeException("Parsing failed", x);
91 } catch(SAXException x) {
92 throw new RuntimeException("Parsing failed", x);
93 } catch(IOException x) {
94 throw new RuntimeException("Parsing failed", x);
95 } catch(IllegalArgumentException x) {
96 throw new RuntimeException("Parsing failed", x);
97 }
98
99 return doc;
100 }
101
102 /**
103 * Writes the content of document <code>doc</code> to the specified output stream.
104 * @param doc The document to be written.
105 * @param out The output stream where the document should be written.
106 * @throws RuntimeException if the operation failed.
107 */
108 public static void
109 writeObject(Document doc, OutputStream out) {
110 try {
111 TransformerFactory factory = TransformerFactory.newInstance();
112 Transformer transformer = factory.newTransformer();
113 DOMSource source = new DOMSource(doc);
114 StreamResult result = new StreamResult(out);
115 transformer.transform(source, result);
116 } catch(TransformerConfigurationException x) {
117 throw new RuntimeException("Failed to write the document!", x);
118 } catch(TransformerException x) {
119 throw new RuntimeException("Failed to write the document!", x);
120 }
121 }
122
123 /**
124 * Validates a text node.
125 * @throws IllegalArgumentException If the node is not a text node.
126 */
127 public static void
128 validateTextContent(Node node) throws IllegalArgumentException {
129 if (
130 node.getChildNodes().getLength() != 1 ||
131 node.getFirstChild().getNodeType() != Node.TEXT_NODE
132 ) { throw new IllegalArgumentException(node.getNodeName() + ": Not a text node"); }
133 }
134
135 /**
136 * Validates a text attribute.
137 * @throws IllegalArgumentException If the node is not a text attribute.
138 */
139 public static void
140 validateTextAttr(Node node) throws IllegalArgumentException {
141 if (node.getNodeType() != Node.ATTRIBUTE_NODE)
142 { throw new IllegalArgumentException(node.getNodeName() + ": Not an attribute"); }
143 }
144 }

  ViewVC Help
Powered by ViewVC