Search in sources :

Example 1 with LocatedAttributeAdapter

use of org.sonar.maven.model.LocatedAttributeAdapter in project sonar-java by SonarSource.

the class PomParser method parseXML.

@CheckForNull
public static MavenProject parseXML(File file) {
    try (FileInputStream is = new FileInputStream(file)) {
        // it is necessary to provide classloader explicitly, otherwise Thread.contextClassLoader will be used,
        // which doesn't include jar of plugin
        JAXBContext context = JAXBContext.newInstance("org.sonar.maven.model.maven2", PomParser.class.getClassLoader());
        XMLInputFactory factory = XMLInputFactory.newInstance();
        enableLocationPropertyForIBM(factory);
        XMLStreamReader reader = factory.createXMLStreamReader(is);
        StreamListener streamListener = new StreamListener(reader);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setListener(streamListener);
        unmarshaller.setAdapter(new LocatedAttributeAdapter(reader));
        JAXBElement<MavenProject> unmarshalledObject = unmarshaller.unmarshal(reader, MavenProject.class);
        if (!"project".equalsIgnoreCase(unmarshalledObject.getName().getLocalPart())) {
            return null;
        }
        return unmarshalledObject.getValue();
    } catch (JAXBException | XMLStreamException | IOException e) {
        LOG.error("Unable to parse pom file " + file.getPath(), e);
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) LocatedAttributeAdapter(org.sonar.maven.model.LocatedAttributeAdapter) MavenProject(org.sonar.maven.model.maven2.MavenProject) XMLStreamException(javax.xml.stream.XMLStreamException) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory) CheckForNull(javax.annotation.CheckForNull)

Aggregations

FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 CheckForNull (javax.annotation.CheckForNull)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 LocatedAttributeAdapter (org.sonar.maven.model.LocatedAttributeAdapter)1 MavenProject (org.sonar.maven.model.maven2.MavenProject)1