use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class RuleSetFactoryTest method testStringMultiPropertyDelimiter.
@Test
public void testStringMultiPropertyDelimiter() throws Exception {
Rule r = loadFirstRule("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset name=\"test\">\n " + " <description>ruleset desc</description>\n " + "<rule name=\"myRule\" message=\"Do not place to this package. Move to \n{0} package/s" + " instead.\" \n" + "class=\"net.sourceforge.pmd.lang.rule.XPathRule\" language=\"dummy\">\n" + " <description>Please move your class to the right folder(rest \nfolder)</description>\n" + " <priority>2</priority>\n <properties>\n <property name=\"packageRegEx\"" + " value=\"com.aptsssss,com.abc\" \ntype=\"List[String]\" delimiter=\",\" " + "description=\"valid packages\"/>\n" + " </properties></rule>" + "</ruleset>");
PropertyDescriptor<List<String>> prop = (PropertyDescriptor<List<String>>) r.getPropertyDescriptor("packageRegEx");
List<String> values = r.getProperty(prop);
assertEquals(Arrays.asList("com.aptsssss", "com.abc"), values);
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class RuleSetFactoryTest method testProps.
@Test
@SuppressWarnings("unchecked")
public void testProps() throws RuleSetNotFoundException {
Rule r = loadFirstRule(PROPERTIES);
assertEquals("bar", r.getProperty((PropertyDescriptor<String>) r.getPropertyDescriptor("fooString")));
assertEquals(new Integer(3), r.getProperty((PropertyDescriptor<Integer>) r.getPropertyDescriptor("fooInt")));
assertTrue(r.getProperty((PropertyDescriptor<Boolean>) r.getPropertyDescriptor("fooBoolean")));
assertEquals(3.0d, r.getProperty((PropertyDescriptor<Double>) r.getPropertyDescriptor("fooDouble")), 0.05);
assertNull(r.getPropertyDescriptor("BuggleFish"));
assertNotSame(r.getDescription().indexOf("testdesc2"), -1);
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class JaxenXPathRuleQuery method createXPath.
private BaseXPath createXPath(final String xpathQueryString, final Navigator navigator) throws JaxenException {
final BaseXPath xpath = new BaseXPath(xpathQueryString, navigator);
if (properties.size() > 1) {
final SimpleVariableContext vc = new SimpleVariableContext();
for (Entry<PropertyDescriptor<?>, Object> e : properties.entrySet()) {
final String propName = e.getKey().name();
if (!"xpath".equals(propName)) {
final Object value = e.getValue();
vc.setVariableValue(propName, value != null ? value.toString() : null);
}
}
xpath.setVariableContext(vc);
}
return xpath;
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd by pmd.
the class RuleSetWriter method createPropertiesElement.
@SuppressWarnings("PMD.CompareObjectsWithEquals")
private Element createPropertiesElement(List<PropertyDescriptor<?>> propertyDescriptors, Map<PropertyDescriptor<?>, Object> propertiesByPropertyDescriptor) {
Element propertiesElement = null;
if (propertyDescriptors != null) {
for (PropertyDescriptor<?> propertyDescriptor : propertyDescriptors) {
if (propertyDescriptor.isDefinedExternally()) {
// Any externally defined property needs to go out as a definition.
if (propertiesElement == null) {
propertiesElement = createPropertiesElement();
}
Element propertyElement = createPropertyDefinitionElementBR(propertyDescriptor);
propertiesElement.appendChild(propertyElement);
} else {
if (propertiesByPropertyDescriptor != null) {
// Otherwise, any property which has a value different than the default needs to go out as a value.
Object defaultValue = propertyDescriptor.defaultValue();
Object value = propertiesByPropertyDescriptor.get(propertyDescriptor);
if (value != defaultValue && (value == null || !value.equals(defaultValue))) {
if (propertiesElement == null) {
propertiesElement = createPropertiesElement();
}
Element propertyElement = createPropertyValueElement(propertyDescriptor, value);
propertiesElement.appendChild(propertyElement);
}
}
}
}
}
if (propertiesByPropertyDescriptor != null) {
// Then, for each PropertyDescriptor not explicitly provided
for (Map.Entry<PropertyDescriptor<?>, Object> entry : propertiesByPropertyDescriptor.entrySet()) {
// If not explicitly given...
PropertyDescriptor<?> propertyDescriptor = entry.getKey();
if (!propertyDescriptors.contains(propertyDescriptor)) {
// Otherwise, any property which has a value different than
// the
// default needs to go out as a value.
Object defaultValue = propertyDescriptor.defaultValue();
Object value = entry.getValue();
if (value != defaultValue && (value == null || !value.equals(defaultValue))) {
if (propertiesElement == null) {
propertiesElement = createPropertiesElement();
}
Element propertyElement = createPropertyValueElement(propertyDescriptor, value);
propertiesElement.appendChild(propertyElement);
}
}
}
}
return propertiesElement;
}
use of net.sourceforge.pmd.properties.PropertyDescriptor in project pmd-eclipse-plugin by pmd.
the class ReportManager method saveReportProperties.
/**
* Save the properties of all renderers to the specified filename.
*
* @param propertyFilename
* String
*/
private static void saveReportProperties(String propertyFilename) {
Properties props = new Properties();
for (Renderer renderer : ReportManager.INSTANCE.allRenderers()) {
Map<PropertyDescriptor<?>, Object> valuesByProp = renderer.getPropertiesByPropertyDescriptor();
for (Map.Entry<PropertyDescriptor<?>, Object> entry : valuesByProp.entrySet()) {
PropertyDescriptor desc = entry.getKey();
props.put(keyOf(renderer, desc), desc.asDelimitedString(entry.getValue()));
}
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(propertyFilename);
props.storeToXML(fos, "asdf");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (Exception e) {
// ignored;
}
}
}
Aggregations