Java Code Examples for java.io.StringReader#close()

The following examples show how to use java.io.StringReader#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Owl2ParserLoaderTest.java    From elk-reasoner with Apache License 2.0 6 votes vote down vote up
private void load(String ontology) throws ElkLoadingException {
	StringReader reader = new StringReader(ontology);
	try {
		Owl2ParserLoader loader = new Owl2ParserLoader(
				DummyInterruptMonitor.INSTANCE,
				new Owl2FunctionalStyleParserFactory().getParser(reader));

		ElkAxiomProcessor dummyProcessor = new ElkAxiomProcessor() {

			@Override
			public void visit(ElkAxiom elkAxiom) {
				// does nothing
			}

		};

		loader.load(dummyProcessor, dummyProcessor);
	} finally {
		reader.close();
	}
}
 
Example 2
Source File: DOM4Parser.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public boolean parse(String input) throws SAXException {
  try {
    DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance();
    domfactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    DocumentBuilder dombuilder = domfactory.newDocumentBuilder();
    StringReader rdr = new StringReader(input);
    InputSource src = new InputSource(rdr);
    Document doc = dombuilder.parse(src);
    doc.getDocumentElement().normalize();
    rdr.close();
    parseresponse(doc.getDocumentElement());
    return true;
  } catch (ParserConfigurationException | IOException e) {
    throw new SAXException(e);
  }
}
 
Example 3
Source File: BasicHTML.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Overridden to return our own slimmed down style sheet.
 */
@Override
public StyleSheet getStyleSheet ()
{
    if ( defaultStyles == null )
    {
        defaultStyles = new StyleSheet ();
        final StringReader r = new StringReader ( styleChanges );
        try
        {
            defaultStyles.loadRules ( r, null );
        }
        catch ( final Exception e )
        {
            // don't want to die in static initialization...
            // just display things wrong.
        }
        r.close ();
        defaultStyles.addStyleSheet ( super.getStyleSheet () );
    }
    return defaultStyles;
}
 
Example 4
Source File: _parsexml.java    From mesh with MIT License 6 votes vote down vote up
public static Record invoke(final String s)
{
    try
    {
        final StringReader reader = new StringReader(s);
        final InputSource source = new InputSource(reader);
        final Document doc = BUILDER.parse(source);
        reader.close();

        return nodeToXNode(doc.getDocumentElement());
    }
    catch (Exception e)
    {
        return makeXNode("$err",
            PersistentMap.EMPTY.assoc("exception", e.getClass().getName()).
                assoc("message", e.getLocalizedMessage()),
            PersistentList.EMPTY);
    }
}
 
Example 5
Source File: SimpleConfigDocument.java    From PlayerVaults with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ConfigDocument withValueText(String path, String newValue) {
    if (newValue == null)
        throw new ConfigException.BugOrBroken("null value for " + path + " passed to withValueText");
    SimpleConfigOrigin origin = SimpleConfigOrigin.newSimple("single value parsing");
    StringReader reader = new StringReader(newValue);
    Iterator<Token> tokens = Tokenizer.tokenize(origin, reader, parseOptions.getSyntax());
    AbstractConfigNodeValue parsedValue = ConfigDocumentParser.parseValue(tokens, origin, parseOptions);
    reader.close();

    return new SimpleConfigDocument(configNodeTree.setValue(path, parsedValue, parseOptions.getSyntax()), parseOptions);
}
 
Example 6
Source File: AuthFile.java    From autorest-clientruntime-for-java with MIT License 5 votes vote down vote up
/**
 * Parses an auth file and read into an AuthFile object.
 * @param file the auth file to read
 * @return the AuthFile object created
 * @throws IOException thrown when the auth file or the certificate file cannot be read or parsed
 */
static AuthFile parse(File file) throws IOException {
    String content = Files.toString(file, Charsets.UTF_8).trim();

    AuthFile authFile;
    if (isJsonBased(content)) {
        authFile = ADAPTER.deserialize(content, AuthFile.class);
        Map<String, String> endpoints = ADAPTER.deserialize(content, new TypeToken<Map<String, String>>() { }.getType());
        authFile.environment.endpoints().putAll(endpoints);
    } else {
        // Set defaults
        Properties authSettings = new Properties();
        authSettings.put(CredentialSettings.AUTH_URL.toString(), AzureEnvironment.AZURE.activeDirectoryEndpoint());
        authSettings.put(CredentialSettings.BASE_URL.toString(), AzureEnvironment.AZURE.resourceManagerEndpoint());
        authSettings.put(CredentialSettings.MANAGEMENT_URI.toString(), AzureEnvironment.AZURE.managementEndpoint());
        authSettings.put(CredentialSettings.GRAPH_URL.toString(), AzureEnvironment.AZURE.graphEndpoint());
        authSettings.put(CredentialSettings.VAULT_SUFFIX.toString(), AzureEnvironment.AZURE.keyVaultDnsSuffix());

        // Load the credentials from the file
        StringReader credentialsReader = new StringReader(content);
        authSettings.load(credentialsReader);
        credentialsReader.close();

        authFile = new AuthFile();
        authFile.clientId = authSettings.getProperty(CredentialSettings.CLIENT_ID.toString());
        authFile.tenantId = authSettings.getProperty(CredentialSettings.TENANT_ID.toString());
        authFile.clientSecret = authSettings.getProperty(CredentialSettings.CLIENT_KEY.toString());
        authFile.clientCertificate = authSettings.getProperty(CredentialSettings.CLIENT_CERT.toString());
        authFile.clientCertificatePassword = authSettings.getProperty(CredentialSettings.CLIENT_CERT_PASS.toString());
        authFile.subscriptionId = authSettings.getProperty(CredentialSettings.SUBSCRIPTION_ID.toString());
        authFile.environment.endpoints().put(Endpoint.MANAGEMENT.identifier(), authSettings.getProperty(CredentialSettings.MANAGEMENT_URI.toString()));
        authFile.environment.endpoints().put(Endpoint.ACTIVE_DIRECTORY.identifier(), authSettings.getProperty(CredentialSettings.AUTH_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.RESOURCE_MANAGER.identifier(), authSettings.getProperty(CredentialSettings.BASE_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.GRAPH.identifier(), authSettings.getProperty(CredentialSettings.GRAPH_URL.toString()));
        authFile.environment.endpoints().put(Endpoint.KEYVAULT.identifier(), authSettings.getProperty(CredentialSettings.VAULT_SUFFIX.toString()));
    }
    authFile.authFilePath = file.getParent();

    return authFile;
}
 
Example 7
Source File: JSON.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json source.
 * @return JSONObject or JSONArray or Boolean or Long or Double or String or null
 * @throws ParseException
 */
public static Object parse(String json) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return parse(reader); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 8
Source File: PathParser.java    From waterdrop with Apache License 2.0 5 votes vote down vote up
static ConfigNodePath parsePathNode(String path, ConfigSyntax flavor) {
    StringReader reader = new StringReader(path);

    try {
        Iterator<Token> tokens = Tokenizer.tokenize(apiOrigin, reader,
                flavor);
        tokens.next(); // drop START
        return parsePathNodeExpression(tokens, apiOrigin, path, flavor);
    } finally {
        reader.close();
    }
}
 
Example 9
Source File: PathAndFillManager.java    From SNT with GNU General Public License v3.0 5 votes vote down vote up
public boolean loadFromString(final String tracesFileAsString) {

		final StringReader reader = new StringReader(tracesFileAsString);
		final boolean result = load(null, reader);
		reader.close();
		return result;

	}
 
Example 10
Source File: PluginDatabaseStore.java    From teiid-spring-boot with Apache License 2.0 5 votes vote down vote up
public Database parse(String ddl) throws IOException {
    StringReader reader = new StringReader(ddl);
    try {
        startEditing(false);
        setMode(Mode.ANY);
        QueryParser.getQueryParser().parseDDL(this, reader);
        return getDatabases().get(0);
    } finally {
        reader.close();
        stopEditing();
    }
}
 
Example 11
Source File: JSON.java    From ROSBridgeClient with GNU General Public License v3.0 5 votes vote down vote up
private static JSONObject convertStringToJSONObject(String json) {
    JSONObject result = null;
    StringReader r = new StringReader(json);
    JSONParser jp = new JSONParser();
    try {
        result = (JSONObject) jp.parse(r);
    }
    catch (Throwable t) {
        System.out.println(t.getMessage());
    }
    r.close();        
    return result;
}
 
Example 12
Source File: TestINIConfiguration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the specified content into the given configuration instance.
 *
 * @param instance the configuration
 * @param data the data to be loaded
 * @throws ConfigurationException if an error occurs
 */
private static void load(final INIConfiguration instance, final String data)
        throws ConfigurationException
{
    final StringReader reader = new StringReader(data);
    try
    {
        instance.read(reader);
    }
    catch (final IOException e)
    {
        throw new ConfigurationException(e);
    }
    reader.close();
}
 
Example 13
Source File: JSON.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * parse json.
 *
 * @param json json string.
 * @param type target type.
 * @return result.
 * @throws ParseException
 */
public static <T> T parse(String json, Class<T> type) throws ParseException {
    StringReader reader = new StringReader(json);
    try {
        return parse(reader, type);
    } catch (IOException e) {
        throw new ParseException(e.getMessage());
    } finally {
        reader.close();
    }
}
 
Example 14
Source File: RestConfigurationImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * @see org.sakaiproject.api.app.help.RestConfiguration#getResourceNameFromCorpusDoc(java.lang.String)
 */
public String getResourceNameFromCorpusDoc(String xml)
{
  try
  {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    StringReader sReader = new StringReader(xml);
    InputSource inputSource = new org.xml.sax.InputSource(sReader);
    org.w3c.dom.Document xmlDocument = builder.parse(inputSource);
    sReader.close();
    
    NodeList nodeList = xmlDocument.getElementsByTagName("kbq");
    
    int nodeListLength = nodeList.getLength();
    for (int i = 0; i < nodeListLength; i++){
      Node currentNode = nodeList.item(i);
      
      NodeList nlChildren = currentNode.getChildNodes();
      
      for (int j = 0; j < nlChildren.getLength(); j++){
        if (nlChildren.item(j).getNodeType() == Node.TEXT_NODE){
          return nlChildren.item(j).getNodeValue();
        }
      }
    }
    return null;
  }            
  catch (Exception e)
  {
    log.error(e.getMessage(), e);
  }     
  
  return null;
}
 
Example 15
Source File: JSON.java    From dubbox with Apache License 2.0 3 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json string.
 * @param types target type array.
 * @return result.
 * @throws ParseException
 */
public static Object[] parse(String json, Class<?>[] types) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return (Object[])parse(reader, types); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 16
Source File: JSON.java    From dubbox-hystrix with Apache License 2.0 3 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json string.
 * @param handler handler.
 * @return result.
 * @throws ParseException
 */
public static Object parse(String json, JSONVisitor handler) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return parse(reader, handler); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 17
Source File: JSON.java    From dubbox with Apache License 2.0 3 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json string.
 * @param type target type.
 * @return result.
 * @throws ParseException
 */
public static <T> T parse(String json, Class<T> type) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return parse(reader, type); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 18
Source File: JSON.java    From dubbox with Apache License 2.0 3 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json string.
 * @param type target type.
 * @return result.
 * @throws ParseException
 */
public static <T> T parse(String json, Class<T> type) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return parse(reader, type); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 19
Source File: JSON.java    From dubbo3 with Apache License 2.0 3 votes vote down vote up
/**
 * parse json.
 * 
 * @param json json string.
 * @param handler handler.
 * @return result.
 * @throws ParseException
 */
public static Object parse(String json, JSONVisitor handler) throws ParseException
{
	StringReader reader = new StringReader(json);
	try{ return parse(reader, handler); }
	catch(IOException e){ throw new ParseException(e.getMessage()); }
	finally{ reader.close(); }
}
 
Example 20
Source File: JavaParser.java    From effectivejava with Apache License 2.0 3 votes vote down vote up
/**
 * Parses the Java import contained in a {@link String} and returns a
 * {@link ImportDeclaration} that represents it.
 * 
 * @param importDeclaration
 *            {@link String} containing Java import code
 * @return ImportDeclaration representing the Java import declaration
 * @throws ParseException
 *             if the source code has parser errors
 * @throws java.io.IOException
 */
public static ImportDeclaration parseImport(final String importDeclaration)
		throws ParseException {
	StringReader sr = new StringReader(importDeclaration);
	ImportDeclaration id = new ASTParser(sr).ImportDeclaration();
	sr.close();
	return id;
}