Example usage for java.io StringReader close

List of usage examples for java.io StringReader close

Introduction

In this page you can find the example usage for java.io StringReader close.

Prototype

public void close() 

Source Link

Document

Closes the stream and releases any system resources associated with it.

Usage

From source file:com.acciente.commons.htmlform.Parser.java

private static Map parseGETParams(HttpServletRequest oRequest) throws ParserException, IOException {
    Map oGETParams;/*from ww w  . ja v a  2s .c  o m*/
    StringReader oQueryStringReader = null;

    try {
        oQueryStringReader = new StringReader(oRequest.getQueryString());

        oGETParams = Parser.parseForm(oQueryStringReader, true);
    } finally {
        if (oQueryStringReader != null) {
            oQueryStringReader.close();
        }
    }

    return oGETParams;
}

From source file:org.andrewberman.sync.InheritMe.java

public static void writeFile(File f, String text) throws Exception {
    FileWriter fw = new FileWriter(f);
    StringReader read = new StringReader(text);
    int c;/* w  w  w  .  j a v a 2 s  . c  om*/
    while ((c = read.read()) != -1) {
        fw.write(c);
    }
    fw.close();
    read.close();
}

From source file:Main.java

/**
 * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree
 *
 * @param dummyJAXBObject/*  w  w w .  ja v  a 2s. com*/
 *            Dummy contect object for creating related JAXB context
 * @param doc
 *            XML DOM document
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD)
        throws Exception {
    if (dummyJAXBObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader reader = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        reader = new StringReader(strXSD);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(doc);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return unmarshalledObject;
}

From source file:io.datalayer.conf.HierarchicalIniConfigurationTest.java

/**
 * Loads the specified content into the given configuration instance.
 *
 * @param instance the configuration/*from  w ww  .  j  a v a  2s  .  c om*/
 * @param data the data to be loaded
 * @throws ConfigurationException if an error occurs
 */
private static void load(HierarchicalINIConfiguration instance, String data) throws ConfigurationException {
    StringReader reader = new StringReader(data);
    instance.load(reader);
    reader.close();
}

From source file:org.kalypso.commons.io.VFSUtilities.java

/**
 * This function copies a string to a vfs file object.
 *
 * @param value/*from  w  w w .j  av a 2s . co m*/
 *          This string will be copied to the file.
 * @param destination
 *          The destination. It must be a file.
 */
public static void copyStringToFileObject(final String value, final FileObject destination) throws IOException {
    if (FileType.FOLDER.equals(destination.getType()))
        throw new IllegalArgumentException("Destination is a folder."); //$NON-NLS-1$

    /* Copy the string to this url. */
    OutputStream outputStream = null;
    StringReader stringReader = null;

    try {
        outputStream = destination.getContent().getOutputStream(false);
        stringReader = new StringReader(value);
        IOUtils.copy(stringReader, outputStream);
        outputStream.close();
        stringReader.close();
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(stringReader);
    }
}

From source file:Main.java

/**
 * Unmarshal XML data from XML string using XSD string and return the resulting JAXB content tree
 *
 * @param dummyCtxObject//from www  .  j a va  2s.  c  o  m
 *            Dummy contect object for creating related JAXB context
 * @param strXML
 *            XML
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshalling(Object dummyCtxObject, String strXML, String strXSD) throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (strXML == null) {
        throw new RuntimeException("No XML document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader readerXSD = null;
    StringReader readerXML = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        readerXSD = new StringReader(strXSD);
        readerXML = new StringReader(strXML);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(readerXSD));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(new StreamSource(readerXML));
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (readerXSD != null) {
            readerXSD.close();
            readerXSD = null;
        }
        if (readerXML != null) {
            readerXML.close();
            readerXML = null;
        }
    }

    return unmarshalledObject;
}

From source file:Pathway2Rdf.java

public static void addPathway2Rdf(String wpIdentifier, String wpRevision, String gpml)
        throws DOMException, XPathExpressionException, ServiceException, ConverterException,
        ParserConfigurationException, SAXException, IOException, ParseException, NullPointerException {

    Model model = ModelFactory.createDefaultModel();
    // Declare the Prefixes

    // Model wikipathways level into RDF
    Resource wikipathwaysResource = model.createResource(WpNamespaces.nsWikipathways);

    Property xCoordinate = model.createProperty(WpNamespaces.nsWikipathways + "GPML/xCoordinate");
    Property yCoordinate = model.createProperty(WpNamespaces.nsWikipathways + "GPML/yCoordinate");
    Property color = model.createProperty(WpNamespaces.nsWikipathways + "GPML/color");
    Property width = model.createProperty(WpNamespaces.nsWikipathways + "GPML/width");
    Property height = model.createProperty(WpNamespaces.nsWikipathways + "GPML/height");

    Resource wikiPathwaysPaperResource = model.createResource("http://www.ncbi.nlm.nih.gov/pubmed/18651794");
    wikipathwaysResource.addProperty(DCTerms.bibliographicCitation, wikiPathwaysPaperResource);
    Resource wikiPathwaysSparqlEndpointResource = model
            .createResource("http://semantics.bigcat.unimaas.nl:8000/sparql");
    wikipathwaysResource.addProperty(Void.sparqlEndpoint, wikiPathwaysSparqlEndpointResource);
    wikipathwaysResource.addProperty(RDF.type, Void.Dataset);

    String DefinitionURI = WpNamespaces.nsWikipathways + "Definition/";
    Resource wikipathwaysGroupDefinitionResource = model.createResource(DefinitionURI + "Group/");

    // State that GeneProduct can either be a Protein or DNA
    RDFNode[] geneProductelems = new RDFNode[] { Biopax_level3.Dna, Biopax_level3.Protein };
    RDFList geneProductRDFNode = model.createList(geneProductelems);

    String pathwayURI = WpNamespaces.nsWikipathways + "Pathway/" + wpIdentifier + "/";
    //System.out.println();
    //System.out.print(wpIdentifier);
    Resource abstractPathwayResource = model.createResource(pathwayURI);
    //System.out.print(wpRevision);
    String pathwayResourceURI = pathwayURI + wpRevision + "/";
    Resource centralPathwayResource = model.createResource(pathwayResourceURI);
    abstractPathwayResource.addProperty(DCTerms.hasVersion, centralPathwayResource);
    Resource pathwayResource = model.createResource(pathwayResourceURI);

    // A Pathway in Wikipathways is identified by its WP identifier and
    // its revision number;
    Resource pathwayIdentifierResource = model.createResource(
            WpNamespaces.nsIdentifiers + "/WikiPathways/" + wpIdentifier + "/" + wpRevision + "/");
    pathwayIdentifierResource.addProperty(RDFS.label, wpIdentifier);
    pathwayResource.addProperty(DC.identifier, pathwayIdentifierResource);
    pathwayResource.addProperty(RDFS.label, wpIdentifier);

    // PARSE GPML
    if (!gpml.startsWith("{{deleted|") && isValidXML(gpml, wpIdentifier, wpRevision)) {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        StringReader reader = new StringReader(gpml);
        InputSource inputSource = new InputSource(reader);
        Document doc = docBuilder.parse(inputSource);
        reader.close();
        doc.getDocumentElement().normalize();
        try {/*  ww  w  .j  a  v a2 s.co m*/
            // Get the Pathway Nodes
            XPath xPath = XPathFactory.newInstance().newXPath();

            Node pathwayLicense = ((Node) xPath.evaluate("/Pathway/@License", doc, XPathConstants.NODE));
            Node pathwayName = ((Node) xPath.evaluate("/Pathway/@Name", doc, XPathConstants.NODE));
            Node pathwayOrganism = ((Node) xPath.evaluate("/Pathway/@Organism", doc, XPathConstants.NODE));

            // Map the organism to DbPedia
            if (pathwayOrganism != null) {
                Resource dbPediaSpeciesResource = model.createResource(
                        "http://dbpedia.org/page/" + pathwayOrganism.getNodeValue().replace(" ", "_"));
                wikipathwaysResource.addProperty(DC.coverage, dbPediaSpeciesResource);
            }

            // Add pathway level details to the RDF model
            if (pathwayName != null)
                pathwayResource.addProperty(RDFS.label, pathwayName.getNodeValue());
            if (pathwayIdentifierResource != null)
                pathwayResource.addProperty(DC.identifier, pathwayIdentifierResource);
            if (pathwayLicense != null)
                pathwayResource.addProperty(DCTerms.license, pathwayLicense.getNodeValue());
            pathwayResource.addProperty(RDF.type, Biopax_level3.Pathway);
            pathwayResource.addProperty(DCTerms.isPartOf, wikipathwaysResource);

            // Get the Group References by calling the getGroupIds from
            // the
            // wikipathways webservices
            NodeList groupIdsNL = doc.getElementsByTagName("Group");
            Set<String> groupIdsSet = new TreeSet<String>();
            for (int t = 0; t < groupIdsNL.getLength(); t++) {
                groupIdsSet.add(groupIdsNL.item(t).getAttributes().getNamedItem("GroupId").getTextContent());
                // System.out.println(groupIdsNL.item(t).getAttributes()
                // .getNamedItem("GroupId").getTextContent());
            }

            for (String groupRef : groupIdsSet) {
                Resource groupResource = model.createResource(pathwayResourceURI + "Group/" + groupRef);
                groupResource.addProperty(RDF.type, wikipathwaysGroupDefinitionResource);
            }

            // Add pathwayElements to the RDF model
            NodeList dataNodeNL = doc.getElementsByTagName("DataNode");

            // for (PathwayElement pwElm : pathway.getDataObjects()) {
            for (int dnlint = 0; dnlint < dataNodeNL.getLength(); dnlint++) {
                // Only take elements with type DATANODE (genes,
                // proteins,
                // metabolites)
                String datanodeType = null;
                if (dataNodeNL.item(dnlint).getAttributes().getNamedItem("Type") != null) {
                    datanodeType = dataNodeNL.item(dnlint).getAttributes().getNamedItem("Type")
                            .getTextContent();
                }
                String datanodeTextLabel = dataNodeNL.item(dnlint).getAttributes().getNamedItem("TextLabel")
                        .getTextContent();
                String datanodeGraphId = dataNodeNL.item(dnlint).getAttributes().getNamedItem("GraphId")
                        .getTextContent();
                String datanodeGroupRef = null;
                if (dataNodeNL.item(dnlint).getAttributes().getNamedItem("GroupRef") != null) {
                    datanodeGroupRef = dataNodeNL.item(dnlint).getAttributes().getNamedItem("GroupRef")
                            .getTextContent();
                }
                NodeList datanodeChilds = dataNodeNL.item(dnlint).getChildNodes();
                // Get ChildNodes Xref, Graphics

                String datanodeXrefId = null;
                String datanodeXrefDatabase = null;
                String dataNodeGraphicsColor = null;
                Float dataNodeGraphicsCenterX = null;
                Float dataNodeGraphicsCenterY = null;
                Float dataNodeGraphicsWidth = null;
                Float dataNodeGraphicsHeight = null;
                for (int dnchildint = 0; dnchildint < datanodeChilds.getLength(); dnchildint++) {
                    // Xref
                    if (datanodeChilds.item(dnchildint).getNodeName().equals("Xref")) {
                        datanodeXrefId = datanodeChilds.item(dnchildint).getAttributes().getNamedItem("ID")
                                .getTextContent();
                        datanodeXrefDatabase = datanodeChilds.item(dnchildint).getAttributes()
                                .getNamedItem("Database").getTextContent();
                    }
                    //Graphics
                    if (datanodeChilds.item(dnchildint).getNodeName().equals("Graphics")) {
                        if (datanodeChilds.item(dnchildint).getAttributes().getNamedItem("Color") != null) {
                            dataNodeGraphicsColor = datanodeChilds.item(dnchildint).getAttributes()
                                    .getNamedItem("Color").getTextContent();
                        }
                        dataNodeGraphicsCenterX = Float.valueOf(datanodeChilds.item(dnchildint).getAttributes()
                                .getNamedItem("CenterX").getTextContent());
                        dataNodeGraphicsCenterY = Float.valueOf(datanodeChilds.item(dnchildint).getAttributes()
                                .getNamedItem("CenterY").getTextContent());
                        dataNodeGraphicsWidth = Float.valueOf(datanodeChilds.item(dnchildint).getAttributes()
                                .getNamedItem("Width").getTextContent());
                        dataNodeGraphicsHeight = Float.valueOf(datanodeChilds.item(dnchildint).getAttributes()
                                .getNamedItem("Height").getTextContent());
                    }
                }

                // if (pwElm.getObjectType() == ObjectType.DATANODE) {
                Resource pathwayEntity = model
                        .createResource(pathwayResourceURI + "Datanode/" + datanodeGraphId);
                pathwayEntity.addProperty(DCTerms.isPartOf, pathwayResource);
                pathwayEntity.addProperty(RDF.type, Spatial.Feature);
                pathwayEntity.addLiteral(xCoordinate, dataNodeGraphicsCenterX);
                pathwayEntity.addLiteral(yCoordinate, dataNodeGraphicsCenterX);
                if (dataNodeGraphicsColor != null) {
                    pathwayEntity.addLiteral(color, dataNodeGraphicsColor);
                }
                pathwayEntity.addLiteral(width, dataNodeGraphicsWidth);
                pathwayEntity.addLiteral(height, dataNodeGraphicsHeight);

                pathwayEntity.addProperty(RDFS.label, datanodeTextLabel);
                // System.out.println(pwElm.getDataNodeType().toString());
                if (datanodeType != null) {
                    if (datanodeType.equals("Metabolite")) {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.SmallMolecule);
                    } else if (datanodeType.equals("Gene")) {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.Dna);
                    } else if (datanodeType.equals("Protein")) {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.Protein);
                    } else if (datanodeType.equals("GeneProduct")) {
                        pathwayEntity.addProperty(RDF.type, OWL.equivalentClass); // See
                        // for
                        // details:
                        // http://answers.semanticweb.com/questions/12177/how-do-you-map-an-object-to-be-either-a-or-b-but-nothing-else-in-rdf
                        pathwayEntity.addProperty(OWL.unionOf, geneProductRDFNode);
                    } else if (datanodeType.equals("Pathway")) {
                        Resource interactingPathwayResource = model
                                .createResource(pathwayResourceURI + "Interaction/" + datanodeGraphId);
                        interactingPathwayResource.addProperty(RDF.type, Biopax_level3.Interaction);
                        interactingPathwayResource.addProperty(RDF.subject, pathwayResource);
                        interactingPathwayResource.addProperty(RDF.object, model
                                .createResource(WpNamespaces.nsWikipathways + "Pathway/" + datanodeXrefId));
                    } else if (datanodeType.equals("Pathway")) {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.Pathway);
                    } else if (datanodeType.equals("Complex")) {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.Complex);
                    } else {
                        pathwayEntity.addProperty(RDF.type, Biopax_level3.Entity);
                    }
                }

                if (datanodeXrefDatabase != null) {
                    String xRefDataSource = datanodeXrefDatabase.toString().replace(" ", "_");
                    Resource pwElmIdentifierResource = model.createResource(WpNamespaces.nsIdentifiers
                            + "WikiPathways/" + xRefDataSource + "/" + datanodeXrefId);
                    pwElmIdentifierResource.addProperty(RDFS.label, datanodeXrefId);
                    Resource pwElmSourceResource = model
                            .createResource(WpNamespaces.nsIdentifiers + "WikiPathways/" + xRefDataSource);
                    pwElmSourceResource.addProperty(RDFS.label, xRefDataSource);
                    pathwayEntity.addProperty(DC.identifier, pwElmIdentifierResource);
                    pathwayEntity.addProperty(DC.source, pwElmSourceResource);
                }

                if (datanodeGroupRef != null) { // Element is part of a
                    // group
                    pathwayEntity.addProperty(DCTerms.isPartOf,
                            model.getResource(pathwayResourceURI + "Group/" + datanodeGroupRef));
                }
            }

            NodeList lineNL = doc.getElementsByTagName("Line");
            for (int llint = 0; llint < lineNL.getLength(); llint++) {
                // Only take elements with type DATANODE (genes,
                // proteins,
                // metabolites)
                if (lineNL.item(llint).getAttributes().getNamedItem("GraphId") != null) {
                    String lineGraphId = lineNL.item(llint).getAttributes().getNamedItem("GraphId")
                            .getTextContent();

                    // Xref
                    Element LineGraphElement = (Element) lineNL.item(llint);
                    NodeList pointGraphrefs = ((NodeList) xPath.evaluate("//Point/@GraphRef", LineGraphElement,
                            XPathConstants.NODESET));
                    String leftLineGraphRefs = null;
                    String rightLineGraphRefs = null;

                    if ((pointGraphrefs.item(0) != null) && ((pointGraphrefs.item(1) != null))) {
                        leftLineGraphRefs = pointGraphrefs.item(0).getTextContent();
                        rightLineGraphRefs = pointGraphrefs.item(1).getTextContent();
                    }
                    if ((leftLineGraphRefs != null) && (rightLineGraphRefs != null)) {

                        String startGroupOrDatanode;
                        String endGroupOrDatanode;

                        // if
                        // (pwElm.getObjectType().equals(ObjectType.LINE)) {
                        Resource pathwayLine = model
                                .createResource(pathwayResourceURI + "Interaction/" + lineGraphId);
                        pathwayLine.addProperty(RDF.type, Biopax_level3.Interaction);
                        if (groupIdsSet.contains(leftLineGraphRefs)) {
                            startGroupOrDatanode = "Group/";
                        } else {
                            startGroupOrDatanode = "Datanode/";
                        }
                        if (groupIdsSet.contains(rightLineGraphRefs)) {
                            endGroupOrDatanode = "Group/";
                        } else {
                            endGroupOrDatanode = "Datanode/";
                        }
                        pathwayLine.addProperty(RDF.predicate, model
                                .getResource(pathwayResourceURI + startGroupOrDatanode + leftLineGraphRefs));
                        pathwayLine.addProperty(RDF.predicate, model
                                .getResource(pathwayResourceURI + endGroupOrDatanode + rightLineGraphRefs));
                    }
                }
            }
            // TODO Seek a pathway with a state
            /*
             * if (pwElm.getObjectType() == ObjectType.STATE) { Resource
             * pathwayEntity = model .createResource(pathwayResourceURI +
             * "State/" + pwElm.getGraphId()); pathwayEntity
             * .addProperty(DCTerms.isPartOf, pathwayResource);
             * pathwayEntity.addProperty(RDFS.label, pwElm.getTextLabel());
             * } }
             */

            // Get the Biopax References
            NodeList bpRef = doc.getElementsByTagName("BiopaxRef");
            HashMap<String, String> bpRefmap = new HashMap<String, String>();
            if (bpRef != null && bpRef.getLength() > 0) {

                for (int j = 0; j < bpRef.getLength(); j++) {
                    if (bpRef.item(j).getParentNode().getNodeName().equals("DataNode")) {
                        bpRefmap.put(bpRef.item(j).getTextContent(),
                                pathwayResourceURI + "Datanode/" + bpRef.item(j).getParentNode().getAttributes()
                                        .getNamedItem("GraphId").getNodeValue());
                    }

                    if (bpRef.item(j).getParentNode().getNodeName().equals("Pathway")) {
                        bpRefmap.put(bpRef.item(j).getTextContent(), pathwayResourceURI);
                    }
                    if (bpRef.item(j).getParentNode().getNodeName().equals("Line")) {
                        // TODO make sure every entity has a graphId
                        if (bpRef.item(j).getParentNode().getAttributes().getNamedItem("GraphId") != null)
                            bpRefmap.put(bpRef.item(j).getTextContent(),
                                    pathwayResourceURI + "Line/" + bpRef.item(j).getParentNode().getAttributes()
                                            .getNamedItem("GraphId").getNodeValue());
                    }
                    if (bpRef.item(j).getParentNode().getNodeName().equals("State")) {
                        bpRefmap.put(bpRef.item(j).getTextContent(),
                                pathwayResourceURI + "State/" + bpRef.item(j).getParentNode().getAttributes()
                                        .getNamedItem("GraphId").getNodeValue());
                    }
                    if (bpRef.item(j).getParentNode().getNodeName().equals("Group")) {
                        bpRefmap.put(bpRef.item(j).getTextContent(),
                                pathwayResourceURI + "Group/" + bpRef.item(j).getParentNode().getAttributes()
                                        .getNamedItem("GroupId").getNodeValue());
                    }
                }
            }

            NodeList cv = doc.getElementsByTagName("bp:openControlledVocabulary");
            if (cv != null && cv.getLength() > 0) {
                for (int a = 0; a < cv.getLength(); a++) {
                    NodeList cvList = cv.item(a).getChildNodes();
                    String OntologyName = cvList.item(2).getTextContent();
                    String OntologyId = cvList.item(1).getTextContent();
                    if (OntologyName.equals("Pathway Ontology")) {
                        Resource ontologyResource = model
                                .createResource("http://purl.org/obo/owl/PW#" + OntologyId.replace(":", "_"));

                        pathwayResource.addProperty(RDF.type, ontologyResource);
                    }

                    System.out.println(OntologyName + ": " + OntologyId);

                }
            }
            NodeList nl = doc.getElementsByTagName("bp:PublicationXRef");
            System.out.println("test" + nl.getLength());
            if (nl != null && nl.getLength() > 0) {
                System.out.println("test");
                for (int k = 0; k < nl.getLength(); k++) {
                    NodeList refId = nl.item(k).getChildNodes();
                    if (refId.getLength() > 3) {
                        if (refId.item(3).getTextContent().equals("PubMed")
                                && (refId.item(1).getTextContent() != null)) {
                            Resource pubmedEntity = model.createResource(
                                    "http://www.ncbi.nlm.nih.gov/pubmed/" + refId.item(1).getTextContent());
                            pubmedEntity.addProperty(RDF.type, Biopax_level3.Evidence);
                            pubmedEntity.addProperty(DCTerms.identifier, refId.item(1).getTextContent());
                            if (bpRefmap.get(nl.item(k).getAttributes().item(0).getNodeValue()) != null) {

                                Resource tempItem = model.getResource(bpRefmap
                                        .get(nl.item(k).getAttributes().item(0).getNodeValue()).toString());
                                tempItem.addProperty(DCTerms.bibliographicCitation, pubmedEntity);
                            }

                        }
                    } else {
                        System.out.println("PROBLEM with: " + wpIdentifier);
                    }
                }
            }
            nl = doc.getElementsByTagName("bp:PublicationXref");
            System.out.println("test" + nl.getLength());
            if (nl != null && nl.getLength() > 0) {
                System.out.println("test");
                for (int k = 0; k < nl.getLength(); k++) {
                    NodeList refId = nl.item(k).getChildNodes();
                    if (refId.getLength() > 3) {
                        if (refId.item(3).getTextContent().equals("PubMed")
                                && (refId.item(1).getTextContent() != null)) {
                            Resource pubmedEntity = model.createResource(
                                    "http://www.ncbi.nlm.nih.gov/pubmed/" + refId.item(1).getTextContent());
                            pubmedEntity.addProperty(RDF.type, Biopax_level3.Evidence);
                            pubmedEntity.addProperty(DCTerms.identifier, refId.item(1).getTextContent());
                            if (bpRefmap.get(nl.item(k).getAttributes().item(0).getNodeValue()) != null) {

                                Resource tempItem = model.getResource(bpRefmap
                                        .get(nl.item(k).getAttributes().item(0).getNodeValue()).toString());
                                tempItem.addProperty(DCTerms.bibliographicCitation, pubmedEntity);
                            }

                        }
                    } else {
                        System.out.println("PROBLEM with: " + wpIdentifier);
                    }
                }
            }
            nl = doc.getElementsByTagName("bp:publicationXref");
            System.out.println("test" + nl.getLength());
            if (nl != null && nl.getLength() > 0) {
                System.out.println("test");
                for (int k = 0; k < nl.getLength(); k++) {
                    NodeList refId = nl.item(k).getChildNodes();
                    if (refId.getLength() > 3) {
                        if (refId.item(3).getTextContent().equals("PubMed")
                                && (refId.item(1).getTextContent() != null)) {
                            Resource pubmedEntity = model.createResource(
                                    "http://www.ncbi.nlm.nih.gov/pubmed/" + refId.item(1).getTextContent());
                            pubmedEntity.addProperty(RDF.type, Biopax_level3.Evidence);
                            pubmedEntity.addProperty(DCTerms.identifier, refId.item(1).getTextContent());
                            if (bpRefmap.get(nl.item(k).getAttributes().item(0).getNodeValue()) != null) {

                                Resource tempItem = model.getResource(bpRefmap
                                        .get(nl.item(k).getAttributes().item(0).getNodeValue()).toString());
                                tempItem.addProperty(DCTerms.bibliographicCitation, pubmedEntity);
                            }

                        }
                    } else {
                        System.out.println("PROBLEM with: " + wpIdentifier);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    FileOutputStream fout;
    fout = new FileOutputStream("/tmp/WpRDF/" + wpIdentifier + "_" + wpRevision + ".nt");
    model.write(fout, "N-TRIPLE");

}

From source file:com.amalto.workbench.utils.XmlUtil.java

public static String formatXmlSource(String xmlSource) {

    SAXReader reader = new SAXReader();
    StringReader stringReader = new StringReader(xmlSource);
    StringWriter writer = null;/*from w w  w.  j  ava2 s .co  m*/
    XMLWriter xmlwriter = null;
    String result = xmlSource;

    try {
        Document document = reader.read(stringReader);
        writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");//$NON-NLS-1$
        format.setIndentSize(4);
        format.setSuppressDeclaration(true);
        xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        result = writer.toString();
    } catch (Exception e) {

    } finally {

        try {
            if (stringReader != null)
                stringReader.close();

            if (xmlwriter != null)
                xmlwriter.close();

            if (writer != null)
                writer.close();

        } catch (Exception e) {

        }

    }
    return result;

}

From source file:org.unitime.timetable.util.BlobRoomAvailabilityService.java

protected Document receiveResponse() throws IOException, DocumentException {
    try {//w w w  .  j  a  v a  2s.co m
        SessionImplementor session = (SessionImplementor) new _RootDAO().getSession();
        Connection connection = session.getJdbcConnectionAccess().obtainConnection();
        String response = null;
        try {
            CallableStatement call = connection.prepareCall(iResponseSql);
            call.registerOutParameter(1, java.sql.Types.CLOB);
            call.execute();
            response = call.getString(1);
            call.close();
        } finally {
            session.getJdbcConnectionAccess().releaseConnection(connection);
        }
        if (response == null || response.length() == 0)
            return null;
        StringReader reader = new StringReader(response);
        Document document = (new SAXReader()).read(reader);
        reader.close();
        return document;
    } catch (Exception e) {
        sLog.error("Unable to receive response: " + e.getMessage(), e);
        return null;
    } finally {
        _RootDAO.closeCurrentThreadSessions();
    }
}

From source file:architecture.common.license.io.LicenseReader.java

public String prettyPrintLicense(String decryptedLicenseKey) throws LicenseException {
    try {/* w  ww. jav a 2s  .  co m*/
        StringReader reader = new StringReader(decryptedLicenseKey);
        org.dom4j.Document doc = (new SAXReader()).read(reader);
        reader.close();
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setNewlines(true);
        outputFormat.setTrimText(false);
        StringWriter writer = new StringWriter();
        XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
        xmlWriter.write(doc);
        writer.close();
        return writer.toString();
    } catch (Exception e) {
        throw new LicenseException(e);
    }
}