Search in sources :

Example 6 with RDFParseException

use of org.openrdf.rio.RDFParseException in project stanbol by apache.

the class RdfIndexingSource method loadRepositoryConfig.

/**
 * @param repoConfigFile
 * @return
 */
private RepositoryConfig loadRepositoryConfig(File repoConfigFile) {
    Repository configRepo = new SailRepository(new MemoryStore());
    RepositoryConnection con = null;
    try {
        configRepo.initialize();
        con = configRepo.getConnection();
        // We need to load the configuration into a context
        org.openrdf.model.URI configContext = con.getValueFactory().createURI("urn:stanbol.entityhub:indexing.source.sesame:config.context");
        RDFFormat format = Rio.getParserFormatForFileName(repoConfigFile.getName());
        try {
            con.add(new InputStreamReader(new FileInputStream(repoConfigFile), Charset.forName("UTF-8")), baseUri, format, configContext);
        } catch (RDFParseException e) {
            throw new IllegalArgumentException("Unable to parsed '" + repoConfigFile + "' using RDF format '" + format + "'!", e);
        } catch (IOException e) {
            throw new IllegalArgumentException("Unable to access '" + repoConfigFile + "'!", e);
        }
        con.commit();
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to load '" + repoConfigFile + "' to inmemory Sail!", e);
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException e) {
            /* ignore */
            }
        }
    }
    Set<String> repoNames;
    RepositoryConfig repoConfig;
    try {
        repoNames = RepositoryConfigUtil.getRepositoryIDs(configRepo);
        if (repoNames.size() == 1) {
            repoConfig = RepositoryConfigUtil.getRepositoryConfig(configRepo, repoNames.iterator().next());
            repoConfig.validate();
        } else if (repoNames.size() > 1) {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' MUST only contain a single repository configuration!");
        } else {
            throw new IllegalArgumentException("Repository configuration file '" + repoConfigFile + "' DOES NOT contain a repository configuration!");
        }
    } catch (RepositoryException e) {
        throw new IllegalStateException("Unable to read RepositoryConfiguration form the " + "in-memory Sail!", e);
    } catch (RepositoryConfigException e) {
        throw new IllegalArgumentException("Repository Configuration in '" + repoConfigFile + "is not valid!", e);
    } finally {
        try {
            configRepo.shutDown();
        } catch (RepositoryException e) {
        /* ignore */
        }
    }
    if (repoConfig.getRepositoryImplConfig() == null) {
        throw new IllegalArgumentException("Missing RepositoryImpl config for " + "config " + repoConfig.getID() + " of file " + repoConfigFile + "!");
    }
    return repoConfig;
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) RepositoryConfig(org.openrdf.repository.config.RepositoryConfig) InputStreamReader(java.io.InputStreamReader) SailRepository(org.openrdf.repository.sail.SailRepository) RepositoryException(org.openrdf.repository.RepositoryException) IOException(java.io.IOException) RepositoryConfigException(org.openrdf.repository.config.RepositoryConfigException) FileInputStream(java.io.FileInputStream) MemoryStore(org.openrdf.sail.memory.MemoryStore) Repository(org.openrdf.repository.Repository) SailRepository(org.openrdf.repository.sail.SailRepository) URI(org.openrdf.model.URI) RDFFormat(org.openrdf.rio.RDFFormat) RDFParseException(org.openrdf.rio.RDFParseException)

Example 7 with RDFParseException

use of org.openrdf.rio.RDFParseException in project wikidata-query-rdf by wikimedia.

the class WikibaseRepository method collectStatementsFromUrl.

/**
 * Collect TTL statements from single URL.
 * @throws RetryableException if there's a retryable error
 */
private void collectStatementsFromUrl(URI uri, StatementCollector collector, Timer timer) throws RetryableException {
    RDFParser parser = this.rdfParserSupplier.get(collector);
    HttpGet request = new HttpGet(uri);
    log.debug("Fetching rdf from {}", uri);
    try (Timer.Context timerContext = timer.time()) {
        try (CloseableHttpResponse response = client.execute(request)) {
            if (response.getStatusLine().getStatusCode() == 404) {
                throw new WikibaseEntityFetchException(uri, WikibaseEntityFetchException.Type.ENTITY_NOT_FOUND);
            }
            if (response.getStatusLine().getStatusCode() == 204) {
                throw new WikibaseEntityFetchException(uri, WikibaseEntityFetchException.Type.NO_CONTENT);
            }
            if (response.getStatusLine().getStatusCode() >= 300) {
                throw new WikibaseEntityFetchException(uri, WikibaseEntityFetchException.Type.UNEXPECTED_RESPONSE);
            }
            try (InputStream in = streamDumper.wrap(getInputStream(response))) {
                if (in == null) {
                    throw new WikibaseEntityFetchException(uri, WikibaseEntityFetchException.Type.EMPTY_RESPONSE);
                }
                parser.parse(new InputStreamReader(in, UTF_8), uri.toString());
            }
        }
    } catch (UnknownHostException | SocketException | SSLHandshakeException e) {
        // We want to bail on this, since it happens to be sticky for some reason
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RetryableException("Error fetching RDF for " + uri, e);
    } catch (RDFParseException | RDFHandlerException e) {
        throw new ContainedException("RDF parsing error for " + uri, e);
    }
}
Also used : SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) PushbackInputStream(java.io.PushbackInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) ContainedException(org.wikidata.query.rdf.tool.exception.ContainedException) RDFParser(org.openrdf.rio.RDFParser) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) RetryableException(org.wikidata.query.rdf.tool.exception.RetryableException) Timer(com.codahale.metrics.Timer) RDFHandlerException(org.openrdf.rio.RDFHandlerException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RDFParseException(org.openrdf.rio.RDFParseException)

Example 8 with RDFParseException

use of org.openrdf.rio.RDFParseException in project wikidata-query-rdf by wikimedia.

the class RDFChunkDeserializer method deser.

public List<Statement> deser(Reader reader, String mime, String baseUri) {
    List<Statement> stmts = new ArrayList<>();
    StatementCollector collector = new StatementCollector(stmts);
    RDFParser parser = rdfParserSuppliers.forMimeType(mime).get(collector);
    try {
        parser.parse(reader, baseUri);
    } catch (IOException | RDFParseException | RDFHandlerException e) {
        throw new IllegalArgumentException("Cannot parse RDF data", e);
    }
    return stmts;
}
Also used : RDFHandlerException(org.openrdf.rio.RDFHandlerException) Statement(org.openrdf.model.Statement) StatementCollector(org.openrdf.rio.helpers.StatementCollector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RDFParser(org.openrdf.rio.RDFParser) RDFParseException(org.openrdf.rio.RDFParseException)

Example 9 with RDFParseException

use of org.openrdf.rio.RDFParseException in project vcell by virtualcell.

the class XMLMetaDataReader method readFromElement.

@SuppressWarnings("unchecked")
public static void readFromElement(VCMetaData metaData, IdentifiableProvider identifiableProvider, Element metadataElement) throws XmlParseException {
    Element bindingElement = metadataElement.getChild(XMLMetaData.URI_BINDING_LIST_TAG, VCMetaData.nsVCML);
    if (bindingElement != null) {
        // read binding
        List<Element> entryElements = bindingElement.getChildren(XMLMetaData.URI_BINDING_TAG, VCMetaData.nsVCML);
        for (Iterator<Element> iterator = entryElements.iterator(); iterator.hasNext(); ) {
            Element entryElement = iterator.next();
            try {
                String uri = entryElement.getAttributeValue(XMLMetaData.URI_ATTR_TAG);
                String vcidString = entryElement.getAttributeValue(XMLMetaData.VCID_ATTR_TAG);
                // make new entry based on URI
                // create VCID
                VCID vcid = VCID.fromString(vcidString);
                // lookup Identifiable object using VCID ... add to entry.
                metaData.getRegistry().newEntry(metaData.getRdfData(), identifiableProvider.getIdentifiableObject(vcid), uri);
            } catch (VCID.InvalidVCIDException e) {
                e.printStackTrace();
                throw new XmlParseException(e);
            }
        }
    }
    Element rdfElement = metadataElement.getChild(XMLRDF.tagRDF, XMLRDF.nsRDF);
    if (rdfElement != null) {
        // read RDF
        try {
            metaData.addToModelFromElement(rdfElement);
        } catch (RDFParseException e) {
            e.printStackTrace();
            throw new XmlParseException(e.getMessage());
        } catch (RDFHandlerException e) {
            e.printStackTrace();
            throw new XmlParseException(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            throw new XmlParseException(e.getMessage());
        }
    }
    Element nonRDFAnnotationListElement = metadataElement.getChild(XMLMetaData.NONRDF_ANNOTATION_LIST_TAG, VCMetaData.nsVCML);
    if (nonRDFAnnotationListElement != null) {
        List<Element> nonRDFAnnotationElements = nonRDFAnnotationListElement.getChildren(XMLMetaData.NONRDF_ANNOTATION_TAG, VCMetaData.nsVCML);
        for (Element nonRDFAnnotationElement : nonRDFAnnotationElements) {
            String vcidString = nonRDFAnnotationElement.getAttributeValue(XMLMetaData.VCID_ATTR_TAG);
            VCID vcid = null;
            try {
                vcid = VCID.fromString(vcidString);
            } catch (InvalidVCIDException e) {
                e.printStackTrace();
                throw new XmlParseException(e.getMessage());
            }
            Identifiable identifiable = identifiableProvider.getIdentifiableObject(vcid);
            if (identifiable != null) {
                // populate the annotation
                Element freeTextAnnotationElement = nonRDFAnnotationElement.getChild(XMLMetaData.FREETEXT_TAG, VCMetaData.nsVCML);
                if (freeTextAnnotationElement != null) {
                    String freeText = freeTextAnnotationElement.getText();
                    metaData.setFreeTextAnnotation(identifiable, freeText);
                }
                Element xhtmlNotesElement = nonRDFAnnotationElement.getChild(XMLMetaData.NOTES_TAG, VCMetaData.nsVCML);
                if (xhtmlNotesElement != null) {
                    metaData.setXhtmlNotes(identifiable, xhtmlNotesElement);
                }
                Element annotationListElement = nonRDFAnnotationElement.getChild(XMLMetaData.ANNOTATION_LIST_TAG, VCMetaData.nsVCML);
                if (annotationListElement != null) {
                    List<?> annotationContents = annotationListElement.getContent();
                    List<Element> annotationElements = new ArrayList<Element>();
                    for (int i = 0; i < annotationContents.size(); i++) {
                        if (annotationContents.get(i) instanceof Element) {
                            annotationElements.add((Element) annotationContents.get(i));
                        }
                    }
                    metaData.setXmlAnnotations(identifiable, annotationElements.toArray(new Element[annotationElements.size()]));
                }
            } else {
                System.err.println("Cannot find identifiable for vcid : " + vcidString);
            }
        }
    }
}
Also used : VCID(cbit.vcell.biomodel.meta.VCID) Element(org.jdom.Element) ArrayList(java.util.ArrayList) XmlParseException(cbit.vcell.xml.XmlParseException) IOException(java.io.IOException) InvalidVCIDException(cbit.vcell.biomodel.meta.VCID.InvalidVCIDException) Identifiable(org.vcell.util.document.Identifiable) RDFHandlerException(org.openrdf.rio.RDFHandlerException) InvalidVCIDException(cbit.vcell.biomodel.meta.VCID.InvalidVCIDException) RDFParseException(org.openrdf.rio.RDFParseException)

Example 10 with RDFParseException

use of org.openrdf.rio.RDFParseException in project vcell by virtualcell.

the class SBMLAnnotationUtil method readAnnotation.

/**
 * readAnnotation : reads SBML annotations and writes it to corresponding 'identifiables' in vcMetaData. Everything except <VCellRelatedInfo>
 * 				is read here.
 * @param identifiable - vcReaction, vcSpecies, vcCompartment, vcBiomodel
 * @param sBase - corresponding SBML elements
 * @throws XMLStreamException
 * @throws
 * @throws RDFHandlerException
 * @throws RDFParseException
 */
public void readAnnotation(Identifiable identifiable, SBase sBase) throws XMLStreamException {
    readMetaID(identifiable, sBase);
    XMLNode annotationRoot = sBase.getAnnotation().getFullAnnotation();
    if (annotationRoot != null) {
        long childCount = annotationRoot.getNumChildren();
        for (int i = 0; i < childCount; ++i) {
            XMLNode annotationBranch = annotationRoot.getChild(i);
            String namespace = annotationBranch.getNamespaceURI(annotationBranch.getPrefix());
            if (namespace != null) {
                if (namespace.equals(DefaultNameSpaces.RDF.uri)) {
                    // read in RDF annotation
                    String text = annotationBranch.toXMLString();
                    // TODO this is a hack to be replaced by proper URI management.
                    text = text.replace("about=\"#", "about=\"");
                    Graph rdfNew = new HashGraph();
                    Map<String, String> nsMap = new HashMap<String, String>();
                    try {
                        SesameRioUtil.readRDFFromString(text, rdfNew, nsMap, RDFFormat.RDFXML, nsSBML);
                    } catch (RDFParseException e) {
                        e.printStackTrace();
                    } catch (RDFHandlerException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    // System.out.println("SBML NS :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(rdfNew, nsSBML));
                    metaData.add(rdfNew);
                // System.out.println("VCML NS :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(metaData.getRdfData(), XMLTags.VCML_NS));
                // System.out.println("SBBox Data :\n" + MIRIAMAnnotationViewer.prettyPrintJenaModel(metaData.getSBbox().getData(), XMLTags.VCML_NS));
                // System.out.println(MIRIAMAnnotationViewer.printResourceMappings(metaData));
                // TODO: old way of reading the text annotations, as application-specific (vCell) annotations. Now we use Notes, which is more compatible with SBML standard
                // } else if(namespace.equals(tripleVCellInfo.getURI()) || namespace.equals(XMLTags.VCML_NS_OLD) ||
                // namespace.equals(XMLTags.VCML_NS)) {
                // int numChildren = (int)annotationBranch.getNumChildren();
                // for (int j = 0; j < numChildren; j++) {
                // XMLNode child = annotationBranch.getChild(j);
                // if (child.isElement() && child.getName().equals(XMLTags.FreeTextAnnotationTag)) {
                // XMLNode contentFreeText = child.getChild(0);
                // // read in the string (not XML string, but character string) from the XMLNode;
                // // set free text annotation for identifiable in metadata.
                // String freeText = contentFreeText.getCharacters();
                // metaData.setFreeTextAnnotation(identifiable, freeText);
                // }
                // }
                } else {
                    // other (tool-specific, non-RDF, XML) annotations
                    Element elementXML = null;
                    // Element annotationElement = null;
                    try {
                        XMLNode clonedAnnotationBranch = annotationBranch.clone();
                        String annotationBranchString = clonedAnnotationBranch.toXMLString();
                        annotationBranchString = annotationBranchString.replace("\t", "");
                        annotationBranchString = annotationBranchString.replace("\r", "");
                        annotationBranchString = annotationBranchString.replace("\n", "");
                        annotationBranchString = annotationBranchString.trim();
                        if (annotationBranchString.isEmpty()) {
                            // TODO: why (and where) are they being generated anyway
                            continue;
                        }
                        XMLNode clonedAnnotRoot = new XMLNode(annotationRoot);
                        clonedAnnotRoot.setNamespaces(annotationRoot.getNamespaces());
                        clonedAnnotRoot.removeChildren();
                        clonedAnnotRoot.addChild(annotationBranch.clone());
                        String str = clonedAnnotRoot.toXMLString();
                        // (XmlUtil.stringToXML(xmlString, null)).getRootElement();
                        elementXML = (XmlUtil.stringToXML(str, null)).getRootElement();
                    } catch (Exception e) {
                    // e.printStackTrace(System.out);
                    // don't do anything .... we want to continue reading in the model, we cannot fail import because annotation is not well-formed.
                    // try wrap in namespace element
                    // System.out.println(sBase.toSBML()+"\n\n"+annotationRoot.toXMLString());
                    // XMLNamespaces xmlNamespaces = sBase.getNamespaces();
                    // if(xmlNamespaces != null && !xmlNamespaces.isEmpty()){
                    // String xmlnsStr = "";
                    // for (int j = 0; j < xmlNamespaces.getNumNamespaces(); j++) {
                    // xmlnsStr+= "\nxmlns"+(xmlNamespaces.getPrefix(j).length()==0?"":":"+xmlNamespaces.getPrefix(j))+"=\""+xmlNamespaces.getURI(j)+"\"";
                    // }
                    // String wrap = "<annotation "+xmlnsStr+">\n"+annotationBranch.toXMLString()+"\n</annotation>";
                    // System.out.println(wrap);
                    // try{
                    // elementXML = (XmlUtil.stringToXML(wrap, null)).getRootElement();
                    // System.out.println("-----PROBLEM FIXED-----");
                    // }catch(Exception e2){
                    // e.printStackTrace();
                    // }
                    // }
                    }
                    // Element elementXML = xmlNodeToElement(annotationBranch);
                    Element[] xmlAnnotations = metaData.getXmlAnnotations(identifiable);
                    Vector<Element> xmlAnnotList = new Vector<Element>();
                    if (xmlAnnotations != null && xmlAnnotations.length > 0) {
                        xmlAnnotList.addAll(Arrays.asList(xmlAnnotations));
                    }
                    if (elementXML != null) {
                        xmlAnnotList.add(elementXML);
                        metaData.setXmlAnnotations(identifiable, xmlAnnotList.toArray(new Element[0]));
                    }
                }
            }
        }
    }
}
Also used : XMLNode(org.sbml.jsbml.xml.XMLNode) HashMap(java.util.HashMap) Element(org.jdom.Element) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) IOException(java.io.IOException) RDFParseException(org.openrdf.rio.RDFParseException) HashGraph(org.sbpax.impl.HashGraph) Graph(org.openrdf.model.Graph) HashGraph(org.sbpax.impl.HashGraph) RDFHandlerException(org.openrdf.rio.RDFHandlerException) Vector(java.util.Vector) RDFParseException(org.openrdf.rio.RDFParseException)

Aggregations

RDFParseException (org.openrdf.rio.RDFParseException)14 IOException (java.io.IOException)13 RDFHandlerException (org.openrdf.rio.RDFHandlerException)10 RepositoryException (org.openrdf.repository.RepositoryException)6 RDFParser (org.openrdf.rio.RDFParser)5 InputStreamReader (java.io.InputStreamReader)4 RDFFormat (org.openrdf.rio.RDFFormat)4 Statement (org.openrdf.model.Statement)3 SailRepository (org.openrdf.repository.sail.SailRepository)3 SocketException (java.net.SocketException)2 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 AccumuloException (org.apache.accumulo.core.client.AccumuloException)2 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)2 RyaClientException (org.apache.rya.api.client.RyaClientException)2