Search in sources :

Example 61 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project intellij-plugins by JetBrains.

the class FileConfigurator method load.

/**
     * @deprecated
     */
public static void load(final ConfigurationBuffer buffer, final Reader r, final String path, final String context, String rootElement) throws ConfigurationException {
    ThreadLocalToolkit.log(new LoadingConfiguration(path));
    Handler h = new Handler(buffer, path, context, rootElement, false);
    SAXParserFactory factory = SAXParserFactory.newInstance();
    try {
        SAXParser parser = factory.newSAXParser();
        InputSource source = new InputSource(r);
        parser.parse(source, h);
    } catch (SAXConfigurationException e) {
        throw e.innerException;
    } catch (SAXParseException e) {
        throw new ConfigurationException.OtherThrowable(e, null, path, e.getLineNumber());
    } catch (Exception e) {
        throw new ConfigurationException.OtherThrowable(e, null, path, -1);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParseException(org.xml.sax.SAXParseException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 62 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jdk8u_jdk by JetBrains.

the class CLDRConverter method convertBundles.

private static void convertBundles(List<Bundle> bundles) throws Exception {
    // Parse SupplementalData file and store the information in the HashMap
    // Calendar information such as firstDay and minDay are stored in
    // supplementalData.xml as of CLDR1.4. Individual territory is listed
    // with its ISO 3166 country code while default is listed using UNM49
    // region and composition numerical code (001 for World.)
    SAXParserFactory factorySuppl = SAXParserFactory.newInstance();
    factorySuppl.setValidating(true);
    SAXParser parserSuppl = factorySuppl.newSAXParser();
    enableFileAccess(parserSuppl);
    handlerSuppl = new SupplementDataParseHandler();
    File fileSupply = new File(SPPL_SOURCE_FILE);
    parserSuppl.parse(fileSupply, handlerSuppl);
    // Parse numberingSystems to get digit zero character information.
    SAXParserFactory numberingParser = SAXParserFactory.newInstance();
    numberingParser.setValidating(true);
    SAXParser parserNumbering = numberingParser.newSAXParser();
    enableFileAccess(parserNumbering);
    handlerNumbering = new NumberingSystemsParseHandler();
    File fileNumbering = new File(NUMBERING_SOURCE_FILE);
    parserNumbering.parse(fileNumbering, handlerNumbering);
    // Parse metaZones to create mappings between Olson tzids and CLDR meta zone names
    SAXParserFactory metazonesParser = SAXParserFactory.newInstance();
    metazonesParser.setValidating(true);
    SAXParser parserMetaZones = metazonesParser.newSAXParser();
    enableFileAccess(parserMetaZones);
    handlerMetaZones = new MetaZonesParseHandler();
    File fileMetaZones = new File(METAZONES_SOURCE_FILE);
    parserNumbering.parse(fileMetaZones, handlerMetaZones);
    // For generating information on supported locales.
    Map<String, SortedSet<String>> metaInfo = new HashMap<>();
    metaInfo.put("LocaleNames", new TreeSet<String>());
    metaInfo.put("CurrencyNames", new TreeSet<String>());
    metaInfo.put("TimeZoneNames", new TreeSet<String>());
    metaInfo.put("CalendarData", new TreeSet<String>());
    metaInfo.put("FormatData", new TreeSet<String>());
    for (Bundle bundle : bundles) {
        // Get the target map, which contains all the data that should be
        // visible for the bundle's locale
        Map<String, Object> targetMap = bundle.getTargetMap();
        EnumSet<Bundle.Type> bundleTypes = bundle.getBundleTypes();
        // resources though the parent resource bundle chain.
        if (bundle.isRoot()) {
            Map<String, Object> enData = new HashMap<>();
            // Create a superset of en-US and en bundles data in order to
            // fill in any missing resources in the base bundle.
            enData.putAll(Bundle.getBundle("en").getTargetMap());
            enData.putAll(Bundle.getBundle("en_US").getTargetMap());
            for (String key : enData.keySet()) {
                if (!targetMap.containsKey(key)) {
                    targetMap.put(key, enData.get(key));
                }
            }
            // Add DateTimePatternChars because CLDR no longer supports localized patterns.
            targetMap.put("DateTimePatternChars", "GyMdkHmsSEDFwWahKzZ");
        }
        // Go ahead and generate them.
        if (bundleTypes.contains(Bundle.Type.LOCALENAMES)) {
            Map<String, Object> localeNamesMap = extractLocaleNames(targetMap, bundle.getID());
            if (!localeNamesMap.isEmpty() || bundle.isRoot()) {
                metaInfo.get("LocaleNames").add(toLanguageTag(bundle.getID()));
                bundleGenerator.generateBundle("util", "LocaleNames", bundle.getID(), true, localeNamesMap, BundleType.OPEN);
            }
        }
        if (bundleTypes.contains(Bundle.Type.CURRENCYNAMES)) {
            Map<String, Object> currencyNamesMap = extractCurrencyNames(targetMap, bundle.getID(), bundle.getCurrencies());
            if (!currencyNamesMap.isEmpty() || bundle.isRoot()) {
                metaInfo.get("CurrencyNames").add(toLanguageTag(bundle.getID()));
                bundleGenerator.generateBundle("util", "CurrencyNames", bundle.getID(), true, currencyNamesMap, BundleType.OPEN);
            }
        }
        if (bundleTypes.contains(Bundle.Type.TIMEZONENAMES)) {
            Map<String, Object> zoneNamesMap = extractZoneNames(targetMap, bundle.getID());
            if (!zoneNamesMap.isEmpty() || bundle.isRoot()) {
                metaInfo.get("TimeZoneNames").add(toLanguageTag(bundle.getID()));
                bundleGenerator.generateBundle("util", "TimeZoneNames", bundle.getID(), true, zoneNamesMap, BundleType.TIMEZONE);
            }
        }
        if (bundleTypes.contains(Bundle.Type.CALENDARDATA)) {
            Map<String, Object> calendarDataMap = extractCalendarData(targetMap, bundle.getID());
            if (!calendarDataMap.isEmpty() || bundle.isRoot()) {
                metaInfo.get("CalendarData").add(toLanguageTag(bundle.getID()));
                bundleGenerator.generateBundle("util", "CalendarData", bundle.getID(), true, calendarDataMap, BundleType.PLAIN);
            }
        }
        if (bundleTypes.contains(Bundle.Type.FORMATDATA)) {
            Map<String, Object> formatDataMap = extractFormatData(targetMap, bundle.getID());
            // LocaleData.getAvailableLocales depends on having FormatData bundles around
            if (!formatDataMap.isEmpty() || bundle.isRoot()) {
                metaInfo.get("FormatData").add(toLanguageTag(bundle.getID()));
                bundleGenerator.generateBundle("text", "FormatData", bundle.getID(), true, formatDataMap, BundleType.PLAIN);
            }
        }
        // For testing
        SortedSet<String> allLocales = new TreeSet<>();
        allLocales.addAll(metaInfo.get("CurrencyNames"));
        allLocales.addAll(metaInfo.get("LocaleNames"));
        allLocales.addAll(metaInfo.get("CalendarData"));
        allLocales.addAll(metaInfo.get("FormatData"));
        metaInfo.put("All", allLocales);
    }
    bundleGenerator.generateMetaInfo(metaInfo);
}
Also used : BundleType(build.tools.cldrconverter.BundleGenerator.BundleType) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 63 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project JMRI by JMRI.

the class XMLUtil method createXMLReader.

/**
     * Creates a SAX parser.
     *
     * <p>
     * See {@link #parse} for hints on setting an entity resolver.
     *
     * @param validate       if true, a validating parser is returned
     * @param namespaceAware if true, a namespace aware parser is returned
     *
     * @throws FactoryConfigurationError Application developers should never
     *                                   need to directly catch errors of this
     *                                   type.
     * @throws SAXException              if a parser fulfilling given parameters
     *                                   can not be created
     *
     * @return XMLReader configured according to passed parameters
     */
public static synchronized XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException {
    SAXParserFactory factory = saxes[validate ? 0 : 1][namespaceAware ? 0 : 1];
    if (factory == null) {
        try {
            factory = SAXParserFactory.newInstance();
        } catch (FactoryConfigurationError err) {
            throw err;
        }
        factory.setValidating(validate);
        factory.setNamespaceAware(namespaceAware);
        saxes[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory;
    }
    try {
        return factory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException ex) {
        // NOI18N
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex);
    }
}
Also used : ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 64 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jabref by JabRef.

the class MrDLibImporter method parse.

/**
     * Parses the input from the server to a ParserResult
     * @param input A BufferedReader with a reference to a string with the servers response
     * @throws IOException
     */
private void parse(BufferedReader input) throws IOException {
    // The Bibdatabase that gets returned in the ParserResult.
    BibDatabase bibDatabase = new BibDatabase();
    // The document to parse
    String recommendations = convertToString(input);
    // The sorted BibEntries gets stored here later
    List<BibEntry> bibEntries = new ArrayList<>();
    //Parsing the response with a SAX parser
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        MrDlibImporterHandler handler = new MrDlibImporterHandler();
        try (InputStream stream = new ByteArrayInputStream(recommendations.getBytes())) {
            saxParser.parse(stream, handler);
        } catch (SAXException e) {
            LOGGER.error(e.getMessage(), e);
        }
        List<RankedBibEntry> rankedBibEntries = handler.getRankedBibEntries();
        rankedBibEntries.sort((RankedBibEntry rankedBibEntry1, RankedBibEntry rankedBibEntry2) -> rankedBibEntry1.rank.compareTo(rankedBibEntry2.rank));
        bibEntries = rankedBibEntries.stream().map(e -> e.entry).collect(Collectors.toList());
    } catch (ParserConfigurationException | SAXException e) {
        LOGGER.error(e.getMessage(), e);
    }
    for (BibEntry bibentry : bibEntries) {
        bibDatabase.insertEntry(bibentry);
    }
    parserResult = new ParserResult(bibDatabase);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) ParserResult(org.jabref.logic.importer.ParserResult) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BibDatabase(org.jabref.model.database.BibDatabase) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 65 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project tika by apache.

the class MimeTypesReader method read.

public void read(InputStream stream) throws IOException, MimeTypeException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        SAXParser parser = factory.newSAXParser();
        parser.parse(stream, this);
    } catch (ParserConfigurationException e) {
        throw new MimeTypeException("Unable to create an XML parser", e);
    } catch (SAXException e) {
        throw new MimeTypeException("Invalid type configuration", e);
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9