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());
}
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"));
}
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);
}
}
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);
}
}
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);
}
}
Aggregations