Skip to content

Commit c13058f

Browse files
committed
fixes #1419: Replaced castor with plain JAXB
PMD-Plugin does not work if run with IBM JDK 1.7.0
1 parent 5514adb commit c13058f

File tree

7 files changed

+65
-161
lines changed

7 files changed

+65
-161
lines changed

ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Eclipse Update Site: <https://sourceforge.net/projects/pmd/files/pmd-eclipse/upd
77
## ????: 4.0.8.v????
88

99
* Updated PMD to 5.3.4
10+
* Fixed PMD-Plugin does not work if run with IBM JDK 1.7.0 ([bug #1419](https://sourceforge.net/p/pmd/bugs/1419/))
1011

1112
**API Changes**:
1213

net.sourceforge.pmd.eclipse.plugin/META-INF/MANIFEST.MF

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.6
2121
Bundle-Vendor: %plugin.provider
2222
Bundle-ClassPath: target/lib/aopalliance.jar,
2323
target/lib/asm.jar,
24-
target/lib/castor-core.jar,
25-
target/lib/castor-xml.jar,
2624
target/lib/commons-collections.jar,
2725
target/lib/commons-io.jar,
2826
target/lib/commons-lang.jar,

net.sourceforge.pmd.eclipse.plugin/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
<artifactId>log4j</artifactId>
2626
<version>1.2.17</version>
2727
</dependency>
28-
<dependency>
29-
<groupId>org.codehaus.castor</groupId>
30-
<artifactId>castor-xml</artifactId>
31-
<version>1.3.3</version>
32-
</dependency>
3328
</dependencies>
3429

3530
<build>

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesManagerImpl.java

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,22 @@
3838
import java.io.ByteArrayInputStream;
3939
import java.io.File;
4040
import java.io.IOException;
41-
import java.io.InputStreamReader;
42-
import java.io.Reader;
4341
import java.io.StringReader;
4442
import java.io.StringWriter;
45-
import java.net.URL;
4643
import java.util.ArrayList;
4744
import java.util.HashMap;
4845
import java.util.Iterator;
4946
import java.util.List;
5047
import java.util.Map;
5148

49+
import javax.xml.bind.DataBindingException;
50+
import javax.xml.bind.JAXBContext;
51+
import javax.xml.bind.JAXBElement;
52+
import javax.xml.bind.JAXBException;
53+
import javax.xml.bind.Marshaller;
54+
import javax.xml.transform.Source;
55+
import javax.xml.transform.stream.StreamSource;
56+
5257
import net.sourceforge.pmd.Rule;
5358
import net.sourceforge.pmd.RuleSet;
5459
import net.sourceforge.pmd.RuleSetFactory;
@@ -58,7 +63,6 @@
5863
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties;
5964
import net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager;
6065
import net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException;
61-
import net.sourceforge.pmd.eclipse.util.IOUtil;
6266

6367
import org.apache.commons.io.IOUtils;
6468
import org.apache.log4j.Logger;
@@ -67,12 +71,6 @@
6771
import org.eclipse.core.runtime.CoreException;
6872
import org.eclipse.ui.IWorkingSetManager;
6973
import org.eclipse.ui.PlatformUI;
70-
import org.exolab.castor.mapping.Mapping;
71-
import org.exolab.castor.mapping.MappingException;
72-
import org.exolab.castor.xml.MarshalException;
73-
import org.exolab.castor.xml.Marshaller;
74-
import org.exolab.castor.xml.Unmarshaller;
75-
import org.exolab.castor.xml.ValidationException;
7674

7775
/**
7876
* This class manages the persistence of the ProjectProperies information structure
@@ -84,10 +82,18 @@ public class ProjectPropertiesManagerImpl implements IProjectPropertiesManager {
8482
private static final Logger log = Logger.getLogger(ProjectPropertiesManagerImpl.class);
8583

8684
private static final String PROPERTIES_FILE = ".pmd";
87-
private static final String PROPERTIES_MAPPING = "/net/sourceforge/pmd/eclipse/runtime/properties/impl/mapping.xml";
8885

8986
private final Map<IProject, IProjectProperties> projectsProperties = new HashMap<IProject, IProjectProperties>();
9087

88+
private static final JAXBContext JAXB_CONTEXT = initJaxbContext();
89+
90+
private static JAXBContext initJaxbContext() {
91+
try {
92+
return JAXBContext.newInstance(ProjectPropertiesTO.class);
93+
} catch (JAXBException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
9197
/**
9298
* Load a project properties
9399
*
@@ -169,32 +175,16 @@ private void loadRuleSetFromProject(IProjectProperties projectProperties) throws
169175
}
170176
}
171177

172-
public ProjectPropertiesTO convertProjectPropertiesFromString(String properties)
173-
throws PropertiesException {
174-
ProjectPropertiesTO projectProperties = null;
175-
Reader reader = null;
178+
public ProjectPropertiesTO convertProjectPropertiesFromString(String properties) {
176179
try {
177-
final Mapping mapping = new Mapping(this.getClass().getClassLoader());
178-
final URL mappingSpecUrl = this.getClass().getResource(PROPERTIES_MAPPING);
179-
mapping.loadMapping(mappingSpecUrl);
180-
181-
reader = new StringReader(properties);
182-
final Unmarshaller unmarshaller = new Unmarshaller(mapping);
183-
projectProperties = (ProjectPropertiesTO) unmarshaller.unmarshal(reader);
184-
} catch (MarshalException e) {
185-
throw new PropertiesException(e);
186-
} catch (ValidationException e) {
187-
throw new PropertiesException(e);
188-
} catch (IOException e) {
189-
throw new PropertiesException(e);
190-
} catch (MappingException e) {
191-
throw new PropertiesException(e);
192-
} finally {
193-
IOUtil.closeQuietly(reader);
180+
Source source = new StreamSource(new StringReader(properties));
181+
JAXBElement<ProjectPropertiesTO> element = JAXB_CONTEXT.createUnmarshaller().unmarshal(source, ProjectPropertiesTO.class);
182+
return element.getValue();
183+
} catch (JAXBException e) {
184+
throw new DataBindingException(e);
194185
}
195-
196-
return projectProperties;
197186
}
187+
198188
/**
199189
* Read a project properties from properties file
200190
*
@@ -273,34 +263,21 @@ private void setRuleSetFromProperties(IProjectProperties projectProperties, Rule
273263
projectProperties.setProjectRuleSet(ruleSet);
274264
}
275265

276-
public String convertProjectPropertiesToString(ProjectPropertiesTO projectProperties)
277-
throws PropertiesException {
278-
StringWriter writer = null;
266+
public String convertProjectPropertiesToString(ProjectPropertiesTO projectProperties) {
279267
try {
280-
final Mapping mapping = new Mapping(getClass().getClassLoader());
281-
final URL mappingSpecUrl = getClass().getResource(PROPERTIES_MAPPING);
282-
mapping.loadMapping(mappingSpecUrl);
283-
284-
writer = new StringWriter();
285-
final Marshaller marshaller = new Marshaller();
286-
marshaller.setProperty("org.exolab.castor.indent", "true");
287-
marshaller.setWriter(writer);
288-
marshaller.setMapping(mapping);
289-
marshaller.marshal(projectProperties);
290-
writer.flush();
268+
Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
269+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
270+
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
271+
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
291272

292-
return writer.toString();
273+
StringWriter writer = new StringWriter();
274+
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
275+
marshaller.marshal(projectProperties, writer);
276+
writer.write("\n");
293277

294-
} catch (MarshalException e) {
295-
throw new PropertiesException(e);
296-
} catch (ValidationException e) {
297-
throw new PropertiesException(e);
298-
} catch (IOException e) {
299-
throw new PropertiesException(e);
300-
} catch (MappingException e) {
301-
throw new PropertiesException(e);
302-
} finally {
303-
IOUtil.closeQuietly(writer);
278+
return writer.toString();
279+
} catch (JAXBException e) {
280+
throw new DataBindingException(e);
304281
}
305282
}
306283

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/ProjectPropertiesTO.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
*/
3636
package net.sourceforge.pmd.eclipse.runtime.properties.impl;
3737

38+
import javax.xml.bind.annotation.XmlAccessOrder;
39+
import javax.xml.bind.annotation.XmlAccessorOrder;
40+
import javax.xml.bind.annotation.XmlElement;
41+
import javax.xml.bind.annotation.XmlElementWrapper;
42+
import javax.xml.bind.annotation.XmlRootElement;
43+
import javax.xml.bind.annotation.XmlType;
44+
3845

3946
/**
4047
* This class is a simple data bean to let simply serialize project properties
@@ -43,6 +50,9 @@
4350
* @author Philippe Herlin
4451
*
4552
*/
53+
@XmlRootElement(name = "pmd")
54+
@XmlType(propOrder = {"workingSetName", "ruleSetStoredInProject", "ruleSetFile", "excludePatterns",
55+
"includePatterns", "rules", "includeDerivedFiles", "violationsAsErrors", "fullBuildEnabled"})
4656
public class ProjectPropertiesTO {
4757
private RuleSpecTO[] rules;
4858
private String[] excludePatterns;
@@ -59,6 +69,8 @@ public class ProjectPropertiesTO {
5969
* @return rules an array of RuleSpecTO objects that keep information of rules
6070
* selected for the current project
6171
*/
72+
@XmlElementWrapper(name = "rules")
73+
@XmlElement(name = "rule")
6274
public RuleSpecTO[] getRules() {
6375
return rules;
6476
}
@@ -76,6 +88,8 @@ public void setRules(final RuleSpecTO[] rules) {
7688
* @return an array of String objects for exclude patterns
7789
* for the current project.
7890
*/
91+
@XmlElementWrapper(name = "excludePatterns")
92+
@XmlElement(name = "excludePattern")
7993
public String[] getExcludePatterns() {
8094
return excludePatterns;
8195
}
@@ -93,6 +107,8 @@ public void setExcludePatterns(String[] excludePatterns) {
93107
* @return an array of String objects for include patterns
94108
* for the current project.
95109
*/
110+
@XmlElementWrapper(name = "includePatterns")
111+
@XmlElement(name = "includePattern")
96112
public String[] getIncludePatterns() {
97113
return includePatterns;
98114
}
@@ -110,6 +126,7 @@ public void setIncludePatterns(String[] includePatterns) {
110126
* @return ruleSetStoredInProject tells whether the project use a ruleset
111127
* stored in the project or the global plugin ruleset.
112128
*/
129+
@XmlElement(name = "useProjectRuleSet")
113130
public boolean isRuleSetStoredInProject() {
114131
return ruleSetStoredInProject;
115132
}
@@ -126,6 +143,7 @@ public void setRuleSetStoredInProject(final boolean ruleSetStoredInProject) {
126143
/**
127144
* @return Returns the rule set file.
128145
*/
146+
@XmlElement(name = "ruleSetFile")
129147
public String getRuleSetFile() {
130148
return ruleSetFile;
131149
}
@@ -140,6 +158,7 @@ public void setRuleSetFile(String ruleSetFile) {
140158
/**
141159
* @return workingSetName the name of the project workingSet
142160
*/
161+
@XmlElement(name = "workingSet")
143162
public String getWorkingSetName() {
144163
return workingSetName;
145164
}
@@ -155,6 +174,7 @@ public void setWorkingSetName(final String workingSetName) {
155174
/**
156175
* @return Returns the includeDerivedFiles.
157176
*/
177+
@XmlElement(name = "includeDerivedFiles")
158178
public boolean isIncludeDerivedFiles() {
159179
return this.includeDerivedFiles;
160180
}
@@ -166,6 +186,7 @@ public void setIncludeDerivedFiles(boolean includeDerivedFiles) {
166186
this.includeDerivedFiles = includeDerivedFiles;
167187
}
168188

189+
@XmlElement(name = "violationsAsErrors")
169190
public boolean isViolationsAsErrors() {
170191
return violationsAsErrors;
171192
}
@@ -186,6 +207,7 @@ public void setFullBuildEnabled(boolean fullBuildEnabled) {
186207
* syntactic sugar for accessing this field
187208
* @return true if we should run at full build
188209
*/
210+
@XmlElement(name = "fullBuildEnabled")
189211
public boolean isFullBuildEnabled() {
190212
return fullBuildEnabled;
191213
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/runtime/properties/impl/RuleSpecTO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
*/
3636
package net.sourceforge.pmd.eclipse.runtime.properties.impl;
3737

38+
import javax.xml.bind.annotation.XmlElement;
39+
import javax.xml.bind.annotation.XmlType;
40+
3841
/**
3942
* This class is a simple data bean to help serialize project properties. Is
4043
* used by the ProjectPropertiesTO to handle project selected rules. This
@@ -43,6 +46,7 @@
4346
* @author Philippe Herlin
4447
*
4548
*/
49+
@XmlType(propOrder = {"name", "ruleSetName"})
4650
public class RuleSpecTO {
4751
private String name;
4852
private String ruleSetName;
@@ -72,6 +76,7 @@ public RuleSpecTO(final String name, final String ruleSetName) {
7276
/**
7377
* @return name a rule name
7478
*/
79+
@XmlElement(name = "name")
7580
public String getName() {
7681
return name;
7782
}
@@ -89,6 +94,7 @@ public void setName(final String name) {
8994
/**
9095
* @return ruleSetName the name of ruleset the rule come from
9196
*/
97+
@XmlElement(name = "ruleset")
9298
public String getRuleSetName() {
9399
return ruleSetName;
94100
}

0 commit comments

Comments
 (0)