KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > PseudoAttributes


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import nu.xom.Builder;
28 import nu.xom.Document;
29 import nu.xom.Element;
30 import nu.xom.ParsingException;
31 import nu.xom.ProcessingInstruction;
32
33
34 /**
35  * <p>
36  * A utility class which converts pseudo-attributes in a
37  * <code>ProcessingInstruction</code> object into
38  * real <code>Attribute/code> objects.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class PseudoAttributes {
46
47     public static Element getAttributes(ProcessingInstruction pi)
48       throws ParsingException {
49   
50         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<");
51         sb.append(pi.getTarget());
52         sb.append(" ");
53         sb.append(pi.getValue());
54         sb.append("/>");
55         
56         try {
57             InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(sb.toString().getBytes("UTF-8"));
58             Builder parser = new Builder();
59               
60             // This line will throw a ParsingException if the processing
61
// instruction does not use pseudo-attributes
62
Document doc = parser.build(in);
63             Element root = doc.getRootElement();
64             // detach root element
65
doc.setRootElement(new Element("fauxRoot"));
66             return root;
67         }
68         catch (Exception JavaDoc ex) {
69             throw new ParsingException(pi.toXML()
70              + " is not in pseudo-attribute format.", ex);
71         }
72     
73     }
74   
75 }
76
Popular Tags