Search in sources :

Example 56 with SAXParser

use of javax.xml.parsers.SAXParser in project ACS by ACS-Community.

the class WDALImpl method validateXML.

/**
	 * Check that everything conforms to the schema in given xml. This check
	 * will be done by parser used in DAL.
	 *
	 * @param xml
	 *
	 * @throws AcsJCDBXMLErrorEx
	 */
public void validateXML(String xml) throws AcsJCDBXMLErrorEx {
    try {
        XMLHandler dalSolver = new XMLHandler(true, logger);
        SAXParser saxParser = dalImpl.getSaxParser();
        saxParser.parse(new InputSource(new StringReader(xml)), dalSolver);
        if (dalSolver.m_errorString != null) {
            logger.log(AcsLogLevel.NOTICE, dalSolver.m_errorString);
            AcsJCDBXMLErrorEx e2 = new AcsJCDBXMLErrorEx();
            e2.setErrorString(dalSolver.m_errorString);
            throw e2;
        }
    } catch (AcsJCDBXMLErrorEx e) {
        throw e;
    } catch (Throwable t) {
        t.printStackTrace();
        AcsJCDBXMLErrorEx e2 = new AcsJCDBXMLErrorEx(t);
        throw e2;
    //CDBXMLErrorEx xmlErr = new CDBXMLErrorEx(t.toString());
    //throw xmlErr;
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) AcsJCDBXMLErrorEx(alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx)

Example 57 with SAXParser

use of javax.xml.parsers.SAXParser in project ACS by ACS-Community.

the class CDBDefault method main.

public static void main(String[] args) {
    try {
        strIOR = null;
        if (args.length < 2) {
            System.out.println("Usage: cmd <idl_type> <instance_name> [-d ior -h]");
            return;
        }
        m_logger = ClientLogManager.getAcsLogManager().getLoggerForApplication("CDBDefault", true);
        String in_type = args[0];
        String in_name = args[1];
        curl_allComponents = "MACI/Components";
        curl = curl_allComponents + "/" + in_name;
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("-d")) {
                if (i < args.length - 1) {
                    strIOR = args[++i];
                }
            }
            if (args[i].equals("-h")) {
                System.out.println("Usage: cmd idl_type instance_name [-d ior -h]");
                return;
            }
        }
        if (strIOR == null) {
            strIOR = "corbaloc::" + InetAddress.getLocalHost().getHostName() + ":" + ACSPorts.getCDBPort() + "/CDB";
        }
        // create and initialize the ORB
        orb = ORB.init(new String[0], null);
        WDAL wdal = WDALHelper.narrow(orb.string_to_object(strIOR));
        String xml = wdal.get_DAO(curl_allComponents);
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        XMLHandler xmlSolver = new XMLHandler(false, m_logger);
        saxParser.parse(new InputSource(new StringReader(xml)), xmlSolver);
        if (xmlSolver.m_errorString != null) {
            String info = "XML parser error: " + xmlSolver.m_errorString;
            AcsJCDBXMLErrorEx cdbxmlErr = new AcsJCDBXMLErrorEx();
            //XMLerror xmlErr = new XMLerror(info);
            throw cdbxmlErr;
        }
        setDefault(xmlSolver.m_rootNode, in_type, in_name);
    } catch (AcsJCDBXMLErrorEx e) {
        m_logger.log(AcsLogLevel.NOTICE, "Xml Error", e);
        e.printStackTrace();
    } catch (Exception e) {
        m_logger.log(AcsLogLevel.NOTICE, "Error", e);
        e.printStackTrace();
    }
}
Also used : WDAL(com.cosylab.CDB.WDAL) XMLHandler(com.cosylab.cdb.jdal.XMLHandler) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) AcsJCDBXMLErrorEx(alma.cdbErrType.wrappers.AcsJCDBXMLErrorEx) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 58 with SAXParser

use of javax.xml.parsers.SAXParser in project spring-security-oauth by spring-projects.

the class SparklrServiceImpl method getSparklrPhotoIds.

public List<String> getSparklrPhotoIds() throws SparklrException {
    try {
        InputStream photosXML = new ByteArrayInputStream(sparklrRestTemplate.getForObject(URI.create(sparklrPhotoListURL), byte[].class));
        final List<String> photoIds = new ArrayList<String>();
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setValidating(false);
        parserFactory.setXIncludeAware(false);
        parserFactory.setNamespaceAware(false);
        SAXParser parser = parserFactory.newSAXParser();
        parser.parse(photosXML, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if ("photo".equals(qName)) {
                    photoIds.add(attributes.getValue("id"));
                }
            }
        });
        return photoIds;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalStateException(e);
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 59 with SAXParser

use of javax.xml.parsers.SAXParser in project ACS by ACS-Community.

the class XSDElementTypeResolver method initializeParser.

protected void initializeParser() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    SAXParser saxParser = factory.newSAXParser();
    parser = saxParser.getParser();
    String schemas = DALImpl.getSchemas(root, logger);
    if (schemas == null)
        schemas = "";
    // do urn to file mapping for schemas
    Map<String, File> schemaName2File = new HashMap<String, File>();
    StringTokenizer tokenizer = new StringTokenizer(schemas);
    while (tokenizer.hasMoreTokens()) {
        String urn = tokenizer.nextToken();
        String fileName = tokenizer.nextToken();
        schemaName2File.put(getXSDElementName(urn) + ".xsd", new File(fileName));
    }
    uriResolver = new DALURIResolver(schemaName2File, logger);
}
Also used : StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 60 with SAXParser

use of javax.xml.parsers.SAXParser in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method getStateChangesFromGR.

private Collection<StateChange> getStateChangesFromGR(long lastUpdated) throws IOException, ParserConfigurationException, SAXException, GRTokenExpiredException, GRAnsweredBadRequestException {
    Timing t = new Timing("EntriesRetriever.getStateChangesFromGR()", context);
    String url = getGoogleHost() + "/reader/api/0/stream/items/ids";
    // &s=deleted/user/-/state/com.google/starred";
    url += "?s=user/-/state/com.google/starred";
    url += "&s=user/-/state/com.google/read";
    url += "&s=" + NEWSROB_PINNED_STATE;
    // &s=deleted/user/-/state/com.google/read";
    url += "&n=10000&ot=" + lastUpdated;
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        HttpRequestBase req = createGRRequest(httpClient, url);
        HttpResponse response = executeGRRequest(httpClient, req, true);
        throwExceptionWhenNotStatusOK(response);
        final List<StateChange> stateChanges = new ArrayList<StateChange>(25);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser parser = saxParserFactory.newSAXParser();
        DefaultHandler handler = new SimpleStringExtractorHandler() {

            private String currentAtomId;

            // cache
            String googleUserId = null;

            @Override
            public void receivedString(String localName, String fqn, String s) {
                if ("number".equals(localName)) {
                    long l = Long.parseLong(s);
                    currentAtomId = TAG_GR_ITEM + U.longToHex(l);
                } else if ("string".equals(localName)) {
                    boolean delete = s.startsWith("delete");
                    int state = -1;
                    if (s.endsWith("read"))
                        state = EntriesRetriever.StateChange.STATE_READ;
                    else if (s.endsWith("starred"))
                        state = EntriesRetriever.StateChange.STATE_STARRED;
                    if (state > -1) {
                        EntriesRetriever.StateChange sc = new EntriesRetriever.StateChange(currentAtomId, state, delete ? EntriesRetriever.StateChange.OPERATION_REMOVE : EntriesRetriever.StateChange.OPERATION_ADD);
                        stateChanges.add(sc);
                    }
                }
            }
        };
        parser.parse(NewsRobHttpClient.getUngzippedContent(response.getEntity(), context), handler);
        PL.log("Entries Retriever: Number of state changes=" + stateChanges.size(), context);
        if (NewsRob.isDebuggingEnabled(context))
            PL.log("State Changes: " + stateChanges, context);
        return stateChanges;
    } finally {
        httpClient.close();
        t.stop();
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) SimpleStringExtractorHandler(com.newsrob.util.SimpleStringExtractorHandler) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) DefaultHandler(org.xml.sax.helpers.DefaultHandler) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) SAXParser(javax.xml.parsers.SAXParser) Timing(com.newsrob.util.Timing) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9