Search in sources :

Example 1 with Attributes

use of org.xml.sax.Attributes in project checkstyle by checkstyle.

the class PackageNamesLoaderTest method testPackagesWithDots.

@Test
@SuppressWarnings("unchecked")
public void testPackagesWithDots() throws Exception {
    final Constructor<PackageNamesLoader> constructor = PackageNamesLoader.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    final PackageNamesLoader loader = constructor.newInstance();
    final Attributes attributes = mock(Attributes.class);
    when(attributes.getValue("name")).thenReturn("coding.");
    loader.startElement("", "", "package", attributes);
    loader.endElement("", "", "package");
    final Field field = PackageNamesLoader.class.getDeclaredField("packageNames");
    field.setAccessible(true);
    final Set<String> list = (Set<String>) field.get(loader);
    assertEquals("coding.", list.iterator().next());
}
Also used : Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) Attributes(org.xml.sax.Attributes) Test(org.junit.Test)

Example 2 with Attributes

use of org.xml.sax.Attributes in project android by JetBrains.

the class TypedVariableTest method testParseGlobal.

public void testParseGlobal() throws Exception {
    final Map<String, Object> paramMap = new HashMap<>();
    File xmlFile = new File(FileUtil.join(getTestDataPath(), "templates", "globals.xml"));
    String xml = Files.toString(xmlFile, Charsets.UTF_8);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8.toString()));
    Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8.toString());
    InputSource inputSource = new InputSource(reader);
    inputSource.setEncoding(Charsets.UTF_8.toString());
    SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() {

        @Override
        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            if (Template.TAG_GLOBAL.equals(name)) {
                String id = attributes.getValue(Template.ATTR_ID);
                if (!paramMap.containsKey(id)) {
                    paramMap.put(id, TypedVariable.parseGlobal(attributes));
                }
            }
        }
    });
    assertEquals("I am a string", paramMap.get("thisIsAnImplicitString"));
    assertEquals("I'm a real string!", paramMap.get("thisIsAPinnochioString"));
    assertEquals("I get interpreted as a string", paramMap.get("thisIsAStringByDefault"));
    assertEquals(Integer.valueOf(128), paramMap.get("thisIsAnInteger"));
    assertEquals("123abc", paramMap.get("thisIsAMalformedInteger"));
    assertEquals(Boolean.TRUE, paramMap.get("thisIsATrueBoolean"));
    assertEquals(Boolean.FALSE, paramMap.get("thisIsAFalseBoolean"));
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) HashMap(com.intellij.util.containers.HashMap) Attributes(org.xml.sax.Attributes) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 3 with Attributes

use of org.xml.sax.Attributes in project android by JetBrains.

the class Template method processXml.

private void processXml(@NotNull final RenderingContext context, @NotNull String xml) throws TemplateProcessingException {
    try {
        xml = XmlUtils.stripBom(xml);
        InputSource inputSource = new InputSource(new StringReader(xml));
        SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
                try {
                    Map<String, Object> paramMap = context.getParamMap();
                    if (TAG_PARAMETER.equals(name)) {
                        String id = attributes.getValue(ATTR_ID);
                        if (!paramMap.containsKey(id)) {
                            String value = attributes.getValue(ATTR_DEFAULT);
                            Object mapValue = value;
                            if (value != null && !value.isEmpty()) {
                                String type = attributes.getValue(ATTR_TYPE);
                                if ("boolean".equals(type)) {
                                    mapValue = Boolean.valueOf(value);
                                }
                            }
                            paramMap.put(id, mapValue);
                        }
                    } else if (TAG_GLOBAL.equals(name)) {
                        String id = attributes.getValue(ATTR_ID);
                        if (!paramMap.containsKey(id)) {
                            paramMap.put(id, TypedVariable.parseGlobal(attributes));
                        }
                    } else if (TAG_GLOBALS.equals(name)) {
                        // Handle evaluation of variables
                        File globalsFile = getPath(attributes, ATTR_FILE);
                        if (globalsFile != null) {
                            processFile(context, globalsFile);
                        }
                    // else: <globals> root element
                    } else if (TAG_EXECUTE.equals(name)) {
                        File recipeFile = getPath(attributes, ATTR_FILE);
                        if (recipeFile != null) {
                            executeRecipeFile(context, recipeFile);
                        }
                    } else if (!name.equals("template") && !name.equals("category") && !name.equals("option") && !name.equals(TAG_THUMBS) && !name.equals(TAG_THUMB) && !name.equals(TAG_ICONS) && !name.equals(TAG_DEPENDENCY) && !name.equals(TAG_FORMFACTOR)) {
                        LOG.error("WARNING: Unknown template directive " + name);
                    }
                } catch (TemplateProcessingException e) {
                    throw new SAXException(e);
                }
            }
        });
    } catch (SAXException ex) {
        if (ex.getCause() instanceof TemplateProcessingException) {
            throw (TemplateProcessingException) ex.getCause();
        }
        throw new TemplateProcessingException(ex);
    } catch (ParserConfigurationException ex) {
        throw new TemplateProcessingException(ex);
    } catch (IOException ex) {
        throw new TemplateProcessingException(ex);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Map(java.util.Map) File(java.io.File)

Example 4 with Attributes

use of org.xml.sax.Attributes 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 5 with Attributes

use of org.xml.sax.Attributes in project WordPress-Android by wordpress-mobile.

the class WPHtmlTest method testStartImg.

// This test failed before #685 was fixed (throws a InvocationTargetException)
public void testStartImg() throws NoSuchMethodException, IllegalAccessException {
    SpannableStringBuilder text = new SpannableStringBuilder();
    Attributes attributes = new AttributesImpl();
    HtmlToSpannedConverter converter = new HtmlToSpannedConverter(null, null, null, null, null, null, 0);
    // startImg is private, we use reflection to change accessibility and invoke it from here
    Method method = HtmlToSpannedConverter.class.getDeclaredMethod("startImg", SpannableStringBuilder.class, Attributes.class, WPHtml.ImageGetter.class);
    method.setAccessible(true);
    try {
        method.invoke(converter, text, attributes, null);
    } catch (InvocationTargetException e) {
        assertTrue("startImg failed see #685", false);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) Attributes(org.xml.sax.Attributes) Method(java.lang.reflect.Method) SpannableStringBuilder(android.text.SpannableStringBuilder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Attributes (org.xml.sax.Attributes)279 DefaultHandler (org.xml.sax.helpers.DefaultHandler)74 SAXException (org.xml.sax.SAXException)66 AttributesImpl (org.xml.sax.helpers.AttributesImpl)50 SAXParser (javax.xml.parsers.SAXParser)48 Test (org.junit.Test)46 POSaveFailedException (org.adempiere.pipo.exception.POSaveFailedException)37 InputSource (org.xml.sax.InputSource)33 SAXParserFactory (javax.xml.parsers.SAXParserFactory)30 IOException (java.io.IOException)29 File (java.io.File)22 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)17 ArrayList (java.util.ArrayList)17 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)17 Test (org.junit.jupiter.api.Test)17 XMLReader (org.xml.sax.XMLReader)17 ContentHandler (org.xml.sax.ContentHandler)15 StringReader (java.io.StringReader)12 Transformer (org.apache.sling.rewriter.Transformer)10