-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
478 jqa tries to load rules from jqa report file (#482)
- Loading branch information
1 parent
14c08b8
commit 6a22c6f
Showing
5 changed files
with
118 additions
and
33 deletions.
There are no files selected for viewing
31 changes: 0 additions & 31 deletions
31
core/rule/src/main/java/com/buschmais/jqassistant/core/rule/impl/reader/XmlHelper.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
core/shared/src/main/java/com/buschmais/jqassistant/core/shared/xml/XmlHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.buschmais.jqassistant.core.shared.xml; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.function.Predicate; | ||
|
||
import javax.xml.XMLConstants; | ||
import javax.xml.namespace.QName; | ||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamConstants; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamReader; | ||
import javax.xml.validation.Schema; | ||
import javax.xml.validation.SchemaFactory; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.xml.sax.SAXException; | ||
|
||
/** | ||
* Provides utility functions for working with XML files. | ||
*/ | ||
@Slf4j | ||
public class XmlHelper { | ||
|
||
private final static XMLInputFactory xmlInputFactory; | ||
|
||
static { | ||
xmlInputFactory = XMLInputFactory.newInstance(); | ||
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); | ||
} | ||
|
||
public static XMLInputFactory getXMLInputFactory() { | ||
return xmlInputFactory; | ||
} | ||
|
||
public static boolean rootElementMatches(InputStreamSupplier inputStreamSupplier, Predicate<QName> rootElementPredicate) { | ||
try (InputStream stream = inputStreamSupplier.get()) { | ||
XMLStreamReader reader = xmlInputFactory.createXMLStreamReader(stream); | ||
while (reader.hasNext()) { | ||
if (reader.next() == XMLStreamConstants.START_ELEMENT) { | ||
return rootElementPredicate.test(reader.getName()); | ||
} | ||
} | ||
} catch (XMLStreamException e) { | ||
log.warn("Cannot parse XML file."); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Cannot close XML document.", e); | ||
} | ||
return false; | ||
} | ||
|
||
public interface InputStreamSupplier { | ||
|
||
InputStream get() throws IOException; | ||
|
||
} | ||
|
||
/** | ||
* Return a {@link Schema} instance for the given resource. | ||
* | ||
* @param resource | ||
* The resource. | ||
* @return The {@link Schema} instance. | ||
*/ | ||
public static Schema getSchema(String resource) { | ||
Schema schema; | ||
try { | ||
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | ||
schema = schemaFactory.newSchema(XmlHelper.class.getResource(resource)); | ||
} catch (SAXException e) { | ||
throw new IllegalStateException("Cannot read rules schema.", e); | ||
} | ||
return schema; | ||
} | ||
} |