package pt.utl.ist.sotis.sherparomeo.conversion; import java.io.StringReader; import javax.xml.XMLConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; import pt.utl.ist.sotis.conversion.ConversionException; import pt.utl.ist.sotis.sherpa.Romeoapi; public class SherpaRomeoUnmarshaller { public static Romeoapi parse(StringBuffer strBuf) throws ConversionException { try { JAXBContext jc = JAXBContext.newInstance("pt.utl.ist.sotis.sherpa"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( SherpaRomeoUnmarshaller.class.getResource("/sherpa.xsd")); unmarshaller.setSchema(schema); StreamSource stream = new StreamSource(new StringReader(strBuf.toString())); return (Romeoapi) unmarshaller.unmarshal(stream); } catch (JAXBException e) { throw new ConversionException(e); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return null; } }