001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021package org.biojava.bio.program; 022 023import org.biojava.bio.program.sax.PdbSAXParser; 024import org.biojava.bio.program.xml.SimpleXMLEmitter; 025import org.xml.sax.ContentHandler; 026import org.xml.sax.XMLReader; 027 028/** 029 * <p> 030 * A class that converts Protein Data Bank (PDB) to 031 * XML that will validate against the biojava:MacromolecularStructure DTD. 032 * <p> 033 * <b>Note this code is experimental and subject to change without notice. 034 * </b> 035 * <p> 036 * Copyright © 2000 Cambridge Antibody Technology. 037 * 038 * <p> 039 * Primary author -<ul> 040 * <li>Simon Brocklehurst (CAT) 041 * </ul> 042 * Other authors -<ul> 043 * <li>Tim Dilks (CAT) 044 * <li>Colin Hardman (CAT) 045 * <li>Stuart Johnston (CAT) 046 *</ul> 047 * 048 * This code was first released to the biojava.org project, July 2000. 049 * 050 * @author Cambridge Antibody Technology (CAT) 051 * @version 0.1 052 * 053 * @see org.biojava.bio.program.sax.BlastLikeSAXParser 054 * @see SimpleXMLEmitter 055 */ 056public class PdbToXMLConverter { 057 058 private String oInput; 059 private XMLReader oParser; 060 061 /** 062 * Creates a new <code>BlastToXMLConverter</code> instance. 063 * 064 */ 065 public PdbToXMLConverter(String poInput) { 066 oInput = poInput; 067 } 068 069 public void convert() throws java.io.IOException, 070 org.xml.sax.SAXException { 071 072 //Access functionality of biojava classes through 073 //standard org.xml.sax interfaces... 074 075 /** 076 * Create a SAX Parser that takes the native output 077 * from blast-like bioinformatics software. 078 */ 079 oParser = (XMLReader) new PdbSAXParser(); 080 081 082 /** 083 * Dynamically change configuration of the parser 084 * in regard of Namespace support. Here, 085 * the xml.org/features/namespaces feature is simply "reset" 086 * to its default value for SAX2. 087 * The xml.org/features/namespaces-prefixes feature is 088 * also set to true. This is to ensure that xmlns attributes 089 * are reported by the parser. These are required because we want 090 * to configure the XMLEmitter to output qualified names (see below). 091 */ 092 try { 093 oParser.setFeature("http://xml.org/sax/features/namespaces",true); 094 oParser.setFeature("http://xml.org/sax/features/namespace-prefixes", 095 true); 096 097 } catch (Exception e) { 098 //If an illegal conmbination of features is chosen, 099 //roll back to default settings. Output a warning, 100 //even though this might mess up the output... 101 System.out.println("WARNING: ignoring attempt to set illegal " + 102 "combination of parser features"); 103 } 104 /** 105 * Create an XML ContentHandler. This 106 * implementation of the DocumentHandler 107 * interface simple outputs nicely formatted 108 * XML. Passing a true value to the SimpleXMLEmitter 109 * constructor instructs the ContentHandler 110 * to take QNames from the SAXParser, rather 111 * than LocalNames. 112 */ 113 ContentHandler oHandler = 114 (ContentHandler) new SimpleXMLEmitter(true); 115 116 /** 117 * Give the parser a reference to the ContentHandler 118 * so that it can send SAX2 mesagges. 119 */ 120 oParser.setContentHandler(oHandler); 121 /** 122 * Now make the Parser parse the output from the 123 * blast-like software and emit XML as specificed 124 * by the DocumentHandler. 125 */ 126 oParser.parse(oInput); 127 128 System.out.println(); 129 130 } 131 132}