@Override public <D extends Device> D describe(D undescribedDevice, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Populating device from XML descriptor: " + undescribedDevice); // Read the XML into a mutable descriptor graph SAXParser parser = new SAXParser(); MutableDevice descriptor = new MutableDevice(); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (D) descriptor.build(undescribedDevice); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse device descriptor: " + ex.toString(), ex); } }
protected XMLReader create() { try { SAXParserFactory factory = SAXParserFactory.newInstance(); // Configure factory to prevent XXE attacks factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setXIncludeAware(false); factory.setNamespaceAware(true); if (getSchemaSources() != null) { factory.setSchema(createSchema(getSchemaSources())); } XMLReader xmlReader = factory.newSAXParser().getXMLReader(); xmlReader.setErrorHandler(getErrorHandler()); return xmlReader; } catch (Exception ex) { throw new RuntimeException(ex); } }
protected void switchToParent() { if (parser != null && parent != null) { parser.setContentHandler(parent); attributes = null; } }
public SAXParser(DefaultHandler handler) { this.xr = create(); if (handler != null) xr.setContentHandler(handler); }
public Handler(I instance, SAXParser parser, Handler parent) { this.instance = instance; this.parser = parser; this.parent = parent; if (parser != null) { parser.setContentHandler(this); } }
@Override public <S extends Service> S describe(S undescribedService, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Reading service from XML descriptor"); SAXParser parser = new SAXParser(); MutableService descriptor = new MutableService(); hydrateBasic(descriptor, undescribedService); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (S)descriptor.build(undescribedService.getDevice()); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse service descriptor: " + ex.toString(), ex); } }
@Override public <D extends Device> D describe(D undescribedDevice, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Populating device from XML descriptor: " + undescribedDevice); // Read the XML into a mutable descriptor graph SAXParser parser = new SAXParser(); MutableDevice descriptor = new MutableDevice(); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (D) descriptor.build(undescribedDevice); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse device descriptor: " + ex.toString(), ex); } }
@Override public <S extends Service> S describe(S undescribedService, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Reading service from XML descriptor"); SAXParser parser = new SAXParser(); MutableService descriptor = new MutableService(); hydrateBasic(descriptor, undescribedService); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (S)descriptor.build(undescribedService.getDevice()); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse service descriptor: " + ex.toString(), ex); } }
@Override public <D extends Device> D describe(D undescribedDevice, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Populating device from XML descriptor: " + undescribedDevice); // Read the XML into a mutable descriptor graph SAXParser parser = new SAXParser(); MutableDevice descriptor = new MutableDevice(); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (D) descriptor.build(undescribedDevice); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse device descriptor: " + ex.toString(), ex); } }
@Override public <S extends Service> S describe(S undescribedService, String descriptorXml) throws DescriptorBindingException, ValidationException { if (descriptorXml == null || descriptorXml.length() == 0) { throw new DescriptorBindingException("Null or empty descriptor"); } try { log.fine("Reading service from XML descriptor"); SAXParser parser = new SAXParser(); MutableService descriptor = new MutableService(); hydrateBasic(descriptor, undescribedService); new RootHandler(descriptor, parser); parser.parse( new InputSource( // TODO: UPNP VIOLATION: Virgin Media Superhub sends trailing spaces/newlines after last XML element, need to trim() new StringReader(descriptorXml.trim()) ) ); // Build the immutable descriptor graph return (S)descriptor.build(undescribedService.getDevice()); } catch (ValidationException ex) { throw ex; } catch (Exception ex) { throw new DescriptorBindingException("Could not parse service descriptor: " + ex.toString(), ex); } }