Search in sources :

Example 76 with SAXParser

use of javax.xml.parsers.SAXParser in project android by JetBrains.

the class ServiceXmlParser method parseManifestForPermissions.

private void parseManifestForPermissions(@NotNull File f) {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(f, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String tagName, Attributes attributes) throws SAXException {
                if (tagName.equals(SdkConstants.TAG_USES_PERMISSION) || tagName.equals(SdkConstants.TAG_USES_PERMISSION_SDK_23) || tagName.equals(SdkConstants.TAG_USES_PERMISSION_SDK_M)) {
                    String permission = attributes.getValue(SdkConstants.ANDROID_NS_NAME_PREFIX + SdkConstants.ATTR_NAME);
                    // Most permissions are "android.permission.XXX", so for readability, just remove the prefix if present
                    permission = permission.replace(SdkConstants.ANDROID_PKG_PREFIX + SdkConstants.ATTR_PERMISSION + ".", "");
                    myDeveloperServiceMetadata.addPermission(permission);
                }
            }
        });
    } catch (Exception e) {
        // This method shouldn't crash the user for any reason, as showing permissions is just
        // informational, but log a warning so developers can see if they make a mistake when
        // creating their service.
        LOG.warn("Failed to read permissions from AndroidManifest.xml", e);
    }
}
Also used : Attributes(org.xml.sax.Attributes) SAXParser(javax.xml.parsers.SAXParser) URISyntaxException(java.net.URISyntaxException) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 77 with SAXParser

use of javax.xml.parsers.SAXParser 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 78 with SAXParser

use of javax.xml.parsers.SAXParser 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 79 with SAXParser

use of javax.xml.parsers.SAXParser 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 80 with SAXParser

use of javax.xml.parsers.SAXParser 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

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