下面列出了java.io.StringReader#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
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();
}
}
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);
}
}
/**
* 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;
}
/**
* @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;
}
/**
* 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();
}
}
/**
* 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();
}
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;
}
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();
}
}
public boolean loadFromString(final String tracesFileAsString) {
final StringReader reader = new StringReader(tracesFileAsString);
final boolean result = load(null, reader);
reader.close();
return result;
}
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();
}
}
/**
* 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(); }
}
/**
* 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;
}
@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);
}
/**
* 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(); }
}
/**
* 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(); }
}
/**
* 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(); }
}
/**
* 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(); }
}
/**
* 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(); }
}
/**
* 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;
}