Search in sources :

Example 1 with SimpleXMLParserDocumentException

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException in project BiglyBT by BiglySoftware.

the class TOTorrentXMLDeserialiser method deserialise.

public TOTorrent deserialise(File file) throws TOTorrentException {
    try {
        SimpleXMLParserDocument doc = SimpleXMLParserDocumentFactory.create(file);
        TOTorrent res = decodeRoot(doc);
        return (res);
    } catch (SimpleXMLParserDocumentException e) {
        throw (new TOTorrentException("XML Parse Fails: " + e.getMessage(), TOTorrentException.RT_DECODE_FAILS));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 2 with SimpleXMLParserDocumentException

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException in project BiglyBT by BiglySoftware.

the class SimpleXMLParserDocumentImpl method create.

private void create(InputStream _input_stream) throws SimpleXMLParserDocumentException {
    if (!_input_stream.markSupported()) {
        _input_stream = new BufferedInputStream(_input_stream);
    }
    _input_stream.mark(100 * 1024);
    // prevent the parser from screwing with our stream by closing it
    UncloseableInputStream uc_is = new UncloseableInputStream(_input_stream);
    SimpleXMLParserDocumentException error = null;
    try {
        createSupport(uc_is);
    } catch (SimpleXMLParserDocumentException e) {
        String msg = Debug.getNestedExceptionMessage(e);
        if ((msg.contains("entity") && msg.contains("was referenced")) || msg.contains("entity reference")) {
            try {
                // nasty hack to try and handle HTML entities that some annoying feeds include :(
                _input_stream.reset();
                createSupport(new EntityFudger(_input_stream));
                return;
            } catch (Throwable f) {
                if (f instanceof SimpleXMLParserDocumentException) {
                    error = (SimpleXMLParserDocumentException) f;
                }
            }
        }
        if (error == null) {
            error = e;
        }
        throw (error);
    } finally {
        if (Constants.isCVSVersion() && error != null) {
            try {
                _input_stream.reset();
                String stuff = FileUtil.readInputStreamAsStringWithTruncation(_input_stream, 2014);
                Debug.out("RSS parsing failed for '" + stuff + "': " + Debug.getExceptionMessage(error));
            } catch (Throwable e) {
            }
        }
        try {
            _input_stream.close();
        } catch (Throwable e) {
        }
    }
}
Also used : SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException)

Example 3 with SimpleXMLParserDocumentException

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException in project BiglyBT by BiglySoftware.

the class SimpleXMLParserDocumentImpl method createSupport.

private void createSupport(InputStream input_stream) throws SimpleXMLParserDocumentException {
    try {
        DocumentBuilderFactory dbf = getDBF();
        // Step 2: create a DocumentBuilder that satisfies the constraints
        // specified by the DocumentBuilderFactory
        DocumentBuilder db = dbf.newDocumentBuilder();
        // Set an ErrorHandler before parsing
        OutputStreamWriter errorWriter = new OutputStreamWriter(System.err);
        MyErrorHandler error_handler = new MyErrorHandler(new PrintWriter(errorWriter, true));
        db.setErrorHandler(error_handler);
        db.setEntityResolver(new EntityResolver() {

            @Override
            public InputSource resolveEntity(String publicId, String systemId) {
                try {
                    URL url = new URL(systemId);
                    if (source_url != null) {
                        String net = AENetworkClassifier.categoriseAddress(source_url.getHost());
                        if (net != AENetworkClassifier.AT_PUBLIC) {
                            if (AENetworkClassifier.categoriseAddress(url.getHost()) != net) {
                                return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
                            }
                        }
                    }
                    String host = url.getHost();
                    InetAddress.getByName(host);
                    // try connecting too as connection-refused will also bork XML parsing
                    InputStream is = null;
                    try {
                        URLConnection con = url.openConnection();
                        con.setConnectTimeout(15 * 1000);
                        con.setReadTimeout(15 * 1000);
                        is = con.getInputStream();
                        byte[] buffer = new byte[32];
                        int pos = 0;
                        while (pos < buffer.length) {
                            int len = is.read(buffer, pos, buffer.length - pos);
                            if (len <= 0) {
                                break;
                            }
                            pos += len;
                        }
                        String str = new String(buffer, "UTF-8").trim().toLowerCase(Locale.US);
                        if (!str.contains("<?xml")) {
                            // not straightforward to check for naked DTDs, could be lots of <!-- commentry preamble which of course can occur
                            // in HTML too
                            buffer = new byte[32000];
                            pos = 0;
                            while (pos < buffer.length) {
                                int len = is.read(buffer, pos, buffer.length - pos);
                                if (len <= 0) {
                                    break;
                                }
                                pos += len;
                            }
                            str += new String(buffer, "UTF-8").trim().toLowerCase(Locale.US);
                            if (str.contains("<html") && str.contains("<head")) {
                                throw (new Exception("Bad DTD"));
                            }
                        }
                    } catch (Throwable e) {
                        return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (Throwable e) {
                            }
                        }
                    }
                    return (null);
                } catch (UnknownHostException e) {
                    return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
                } catch (Throwable e) {
                    return (null);
                }
            }
        });
        // Step 3: parse the input file
        document = db.parse(input_stream);
        SimpleXMLParserDocumentNodeImpl[] root_nodes = parseNode(document, false);
        int root_node_count = 0;
        for (int i = 0; i < root_nodes.length; i++) {
            SimpleXMLParserDocumentNodeImpl node = root_nodes[i];
            if (node.getNode().getNodeType() != Node.PROCESSING_INSTRUCTION_NODE) {
                root_node = node;
                root_node_count++;
            }
        }
        if (root_node_count != 1) {
            throw (new SimpleXMLParserDocumentException("invalid document - " + root_nodes.length + " root elements"));
        }
    } catch (Throwable e) {
        throw (new SimpleXMLParserDocumentException(e));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) UnknownHostException(java.net.UnknownHostException) URL(java.net.URL) URLConnection(java.net.URLConnection) UnknownHostException(java.net.UnknownHostException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 4 with SimpleXMLParserDocumentException

use of com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException in project BiglyBT by BiglySoftware.

the class UPnPImpl method parseXML.

public SimpleXMLParserDocument parseXML(InputStream _is) throws SimpleXMLParserDocumentException, IOException {
    // ASSUME UTF-8
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[8192];
        while (true) {
            int len = _is.read(buffer);
            if (len <= 0) {
                break;
            }
            baos.write(buffer, 0, len);
        }
    } finally {
        baos.close();
    }
    byte[] bytes_in = baos.toByteArray();
    InputStream is = new ByteArrayInputStream(bytes_in);
    try {
        StringBuilder data = new StringBuilder(1024);
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        Set ignore_map = null;
        while (true) {
            String line = lnr.readLine();
            if (line == null) {
                break;
            }
            for (int i = 0; i < line.length(); i++) {
                char c = line.charAt(i);
                if (c < 0x20 && c != '\r' && c != '\t') {
                    data.append(' ');
                    if (ignore_map == null) {
                        ignore_map = new HashSet();
                    }
                    Character cha = new Character(c);
                    if (!ignore_map.contains(cha)) {
                        ignore_map.add(cha);
                        adapter.trace("    ignoring character(s) " + (int) c + " in xml response");
                    }
                } else {
                    data.append(c);
                }
            }
            data.append("\n");
        }
        String data_str = data.toString();
        adapter.trace("UPnP:Response:" + data_str);
        try {
            SimpleXMLParserDocument doc = adapter.parseXML(data_str);
            return (doc);
        } catch (Throwable e) {
            if (data_str.contains("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">")) {
                data_str = data_str.replace("<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">", "<scpd>");
                return (adapter.parseXML(data_str));
            }
            throw (e);
        }
    } catch (Throwable e) {
        try {
            FileOutputStream trace = new FileOutputStream(getTraceFile());
            try {
                trace.write(bytes_in);
            } finally {
                trace.close();
            }
        } catch (Throwable f) {
            adapter.log(f);
        }
        if (e instanceof SimpleXMLParserDocumentException) {
            throw ((SimpleXMLParserDocumentException) e);
        }
        throw (new SimpleXMLParserDocumentException(e));
    }
}
Also used : SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)

Aggregations

SimpleXMLParserDocumentException (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException)4 SimpleXMLParserDocument (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 UnknownHostException (java.net.UnknownHostException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1