1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.parsers;
39
40 import org.w3c.dom.Document;
41 import org.w3c.dom.NodeList;
42 import org.xml.sax.ErrorHandler;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.SAXParseException;
45
46 import javax.xml.parsers.DocumentBuilder;
47 import javax.xml.parsers.DocumentBuilderFactory;
48 import javax.xml.parsers.ParserConfigurationException;
49 import java.io.File;
50 import java.io.IOException;
51
52
53
54
55
56
57 public class PDBMLFilter implements ErrorHandler {
58
59 private static final String JAXP_SCHEMA_LANGUAGE =
60 "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
61 private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
62 private static final String JAXP_SCHEMA_SOURCE =
63 "http://java.sun.com/xml/jaxp/properties/schemaSource";
64 private static final String PDBX_XSD = "pdbx.xsd";
65 private File pdbxFile = null;
66
67
68 public PDBMLFilter() {}
69
70
71
72
73
74
75 public static void main(String[] args) {
76 if (args.length != 1) {
77 System.out.println("Usage: java PDBXFilter name.xml");
78 }
79 PDBMLFilter pdbx = new PDBMLFilter();
80 File pdbxFile = new File(args[0]);
81 pdbx.parse(pdbxFile);
82 }
83
84
85 @Override
86 public void error(SAXParseException e) throws SAXException {
87 parseMessage(e);
88 }
89
90
91 @Override
92 public void fatalError(SAXParseException e) throws SAXException {
93 parseMessage(e);
94 }
95
96
97
98
99
100
101 public void parseMessage(Exception e) {
102 System.out.println("Could not parse: " + pdbxFile + "\n" + e);
103 }
104
105
106 @Override
107 public void warning(SAXParseException e) throws SAXException {
108 parseMessage(e);
109 }
110
111 private void parse(File pdbxml) {
112 pdbxFile = pdbxml;
113 try {
114 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
115 dbf.setNamespaceAware(true);
116 dbf.setValidating(true);
117 dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
118 dbf.setAttribute(JAXP_SCHEMA_SOURCE, new File(PDBX_XSD));
119 DocumentBuilder db = dbf.newDocumentBuilder();
120 db.setErrorHandler(this);
121 Document doc = db.parse(pdbxFile);
122 NodeList list = doc.getElementsByTagName("PDBx:atom_site");
123 System.out.println("Number of Atoms: " + list.getLength());
124 } catch (ParserConfigurationException
125 | IllegalArgumentException
126 | SAXException
127 | IOException e) {
128 parseMessage(e);
129 }
130 }
131 }