Search in sources :

Example 1 with CheckResult

use of aQute.bnd.deployer.repository.api.CheckResult in project bnd by bndtools.

the class ObrContentProvider method checkStream.

public CheckResult checkStream(String name, InputStream stream) throws IOException {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ParserState state = ParserState.beforeRoot;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case PROCESSING_INSTRUCTION:
                    if (PI_TARGET_STYLESHEET.equals(reader.getPITarget()) && PI_DATA_STYLESHEET.equals(reader.getPIData()))
                        return new CheckResult(accept, "Recognised stylesheet", null);
                    break;
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    switch(state) {
                        case beforeRoot:
                            String nsUri = reader.getNamespaceURI();
                            if (nsUri != null)
                                return CheckResult.fromBool(NS_URI.equals(nsUri), "Correct namespace on root element", "Incorrect namespace on root element: " + nsUri, null);
                            if (!TAG_REPOSITORY.equals(localName))
                                return new CheckResult(reject, "Incorrect root element name", null);
                            state = ParserState.inRoot;
                            break;
                        case inRoot:
                            if (TAG_RESOURCE.equals(localName)) {
                                state = ParserState.inResource;
                            } else if (!TAG_REFERRAL.equals(localName)) {
                                return new CheckResult(reject, String.format("Incorrect element '%s', expected '%s' or '%s'.", localName, TAG_RESOURCE, TAG_REFERRAL), null);
                            }
                            break;
                        case inResource:
                            if (TAG_CAPABILITY.equals(localName)) {
                                state = ParserState.inCapability;
                            }
                            break;
                        case inCapability:
                            return CheckResult.fromBool(TAG_PROPERTY.equals(localName), "Found 'p' tag inside 'capability'", String.format("Incorrect element '%s' inside '%s'; expected '%s'.", localName, TAG_CAPABILITY, TAG_PROPERTY), null);
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
                        state = ParserState.inRoot;
                    if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
                        state = ParserState.inResource;
                    break;
            }
        }
        return new CheckResult(undecided, "Reached end of stream", null);
    } catch (XMLStreamException e) {
        return new CheckResult(reject, "Invalid XML", e);
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) CheckResult(aQute.bnd.deployer.repository.api.CheckResult) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with CheckResult

use of aQute.bnd.deployer.repository.api.CheckResult in project bnd by bndtools.

the class R5RepoContentProvider method checkStream.

public CheckResult checkStream(String name, InputStream stream) throws IOException {
    XMLStreamReader reader = null;
    try {
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
        inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
        inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        reader = inputFactory.createXMLStreamReader(stream);
        ParserState state = ParserState.beforeRoot;
        while (reader.hasNext()) {
            int type = reader.next();
            String localName;
            switch(type) {
                case START_ELEMENT:
                    localName = reader.getLocalName();
                    switch(state) {
                        case beforeRoot:
                            String nsUri = reader.getNamespaceURI();
                            if (nsUri != null)
                                return CheckResult.fromBool(NS_URI.equals(nsUri), "Corrent namespace", "Incorrect namespace: " + nsUri, null);
                            if (!TAG_REPOSITORY.equals(localName))
                                return new CheckResult(reject, "Incorrect root element name", null);
                            state = ParserState.inRoot;
                            break;
                        case inRoot:
                            if (TAG_RESOURCE.equals(localName)) {
                                state = ParserState.inResource;
                            }
                            break;
                        case inResource:
                            if (TAG_REQUIREMENT.equals(localName))
                                return new CheckResult(accept, "Recognised element 'requirement' in 'resource'", null);
                            if (TAG_CAPABILITY.equals(localName))
                                state = ParserState.inCapability;
                            break;
                        case inCapability:
                            if (TAG_ATTRIBUTE.equals(localName) || TAG_DIRECTIVE.equals(localName)) {
                                return new CheckResult(accept, "Recognised element '%s' in 'capability'", null);
                            }
                            break;
                    }
                    break;
                case END_ELEMENT:
                    localName = reader.getLocalName();
                    if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
                        state = ParserState.inRoot;
                    if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
                        state = ParserState.inResource;
                    break;
                default:
                    break;
            }
        }
        return new CheckResult(undecided, "Reached end of stream", null);
    } catch (XMLStreamException e) {
        return new CheckResult(reject, "Invalid XML", e);
    } finally {
        if (reader != null)
            try {
                reader.close();
            } catch (XMLStreamException e) {
            }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) CheckResult(aQute.bnd.deployer.repository.api.CheckResult) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with CheckResult

use of aQute.bnd.deployer.repository.api.CheckResult in project bnd by bndtools.

the class TestObrRecognition method testAcceptStylesheet.

public static void testAcceptStylesheet() throws Exception {
    String testdata = "<?xml version='1.0' encoding='utf-8'?>" + "<?xml-stylesheet type='text/xsl' href='http://www2.osgi.org/www/obr2html.xsl'?>" + "<repository...";
    ByteArrayInputStream stream = new ByteArrayInputStream(testdata.getBytes());
    CheckResult result = new ObrContentProvider(indexer).checkStream("xxx", stream);
    assertEquals(accept, result.getDecision());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CheckResult(aQute.bnd.deployer.repository.api.CheckResult)

Example 4 with CheckResult

use of aQute.bnd.deployer.repository.api.CheckResult in project bnd by bndtools.

the class TestObrRecognition method testAcceptExtensionElementOtherNamespace.

public static void testAcceptExtensionElementOtherNamespace() throws Exception {
    String testdata;
    ByteArrayInputStream stream;
    CheckResult result;
    // Arbitrary elements under resource are allowed
    testdata = "<?xml version='1.0'?>" + "<repository><resource><foo:XXX xmlns:foo='http://org.example/ns'/><YYY/><capability><p/></capability><ZZZ/></resource><repo...";
    stream = new ByteArrayInputStream(testdata.getBytes());
    result = new ObrContentProvider(indexer).checkStream("xxx", stream);
    assertEquals(accept, result.getDecision());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CheckResult(aQute.bnd.deployer.repository.api.CheckResult)

Example 5 with CheckResult

use of aQute.bnd.deployer.repository.api.CheckResult in project bnd by bndtools.

the class TestObrRecognition method testRejectOnRepositoryChildElementName.

public static void testRejectOnRepositoryChildElementName() throws Exception {
    String testdata;
    ByteArrayInputStream stream;
    CheckResult result;
    // Definitely wrong
    testdata = "<repository><XXX/><repo...";
    stream = new ByteArrayInputStream(testdata.getBytes());
    result = new ObrContentProvider(indexer).checkStream("xxx", stream);
    assertEquals(reject, result.getDecision());
    assertNull(result.getException());
    // Okay but not enough to decide for sure
    testdata = "<repository><resource/></repository>";
    stream = new ByteArrayInputStream(testdata.getBytes());
    result = new ObrContentProvider(indexer).checkStream("xxx", stream);
    assertEquals(undecided, result.getDecision());
    // Okay but not enough to decide for sure
    testdata = "<repository><referral/></repository>";
    stream = new ByteArrayInputStream(testdata.getBytes());
    result = new ObrContentProvider(indexer).checkStream("xxx", stream);
    assertEquals(undecided, result.getDecision());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CheckResult(aQute.bnd.deployer.repository.api.CheckResult)

Aggregations

CheckResult (aQute.bnd.deployer.repository.api.CheckResult)12 ByteArrayInputStream (java.io.ByteArrayInputStream)9 XMLStreamException (javax.xml.stream.XMLStreamException)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 IRepositoryContentProvider (aQute.bnd.deployer.repository.api.IRepositoryContentProvider)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1