📄 ntriple.java
字号:
/*
* (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* * $Id: NTriple.java,v 1.24 2007/01/02 11:49:00 andy_seaborne Exp $
AUTHOR: Jeremy J. Carroll
*/
/*
* XML2NTriple.java
*
* Created on July 13, 2001, 10:06 PM
*/
package com.hp.hpl.jena.rdf.arp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/** A command line interface into ARP.
* Creates NTriple's or just error messages.
* <pre>
* java <class-path> com.hp.hpl.jena.arp.NTriple ( [ -[xstfurR]][ -b xmlBase -[eiw] NNN[,NNN...] ] [ file ] [ url ] )...
* </pre>
* <p>
* All options, files and URLs can be intemingled in any order.
* They are processed from left-to-right.
* <dl>
* file </dt><dd> Converts RDF/XML file into N-triples
* </dd><dt>
* url </dt><dd> Converts RDF/XML from URL into N-triples
* </dd><dt>
* -b uri </dt><dd> Sets XML Base to the absolute URI.
* </dd><dt>
* -r </dt><dd> Content is RDF (default, no embedding, rdf:RDF tag may be omitted).
* </dd><dt>
* -R </dt><dd> RDF embedded in XML document, search for obligatory rdf:RDF start element.
* </dd><dt>
* -t </dt><dd> No n-triple output, error checking only.
* </dd><dt>
* -x </dt><dd> Lax mode - warnings are suppressed.
* </dd><dt>
* -s </dt><dd> Strict mode - most warnings are errors.
* </dd><dt>
* -u </dt><dd> Allow unqualified attributes (defaults to warning).
* </dd><dt>
* -f </dt><dd> All errors are fatal - report first one only.
* </dd><dt>
* -n </dt><dd> Show line numbers of each triple.
* </dd><dt>
* -b url </dt><dd> Sets XML Base to the absolute url.
* </dd><dt>
* -e NNN[,NNN...]</dt><dd>
* Treats numbered warning conditions as errrors.
* </dd><dt>
* -w NNN[,NNN...]</dt><dd>
* Treats numbered error conditions as warnings.
* </dd><dt>
* -i NNN[,NNN...]
* </dt><dd>
* Ignores numbered error/warning conditions.
* </dl>
* @author jjc
*/
public class NTriple implements ARPErrorNumbers {
private static StringBuffer line = new StringBuffer();
private static ARP arp;
private static String xmlBase = null;
private static boolean numbers = false;
/** Starts an RDF/XML to NTriple converter.
* @param args The command-line arguments.
*/
static public void main(String args[]) {
mainEh(args, null, null);
}
static StatementHandler andMeToo = null;
/** Starts an RDF/XML to NTriple converter,
* using an error handler, and an ARPHandler.
* Statements get processed both by this class,
* and by the passed in StatementHandler
* @param args The command-line arguments.
* @param eh Can be null.
* @param ap Can be null.
*/
static public void mainEh(String args[], ErrorHandler eh, ARPEventHandler ap) {
boolean doneOne = false;
startMem = -1;
andMeToo = ap;
//SH sh = new SH();
int i;
arp = new ARP();
ARPHandlers handlers = arp.getHandlers();
handlers.setStatementHandler(getSH(true));
// arp.getOptions().setEmbedding(true);
if (ap != null) {
handlers.setNamespaceHandler(ap);
handlers.setExtendedHandler(ap);
}
if (eh != null)
handlers.setErrorHandler(eh);
for (i = 0; i < args.length - 1; i++) {
if (args[i].startsWith("-")) {
i += processOpts(args[i].substring(1), args[i + 1]);
} else {
doneOne = true;
process(args[i]);
}
}
if (args.length > 0) {
if (args[i].startsWith("-")) {
if (doneOne || processOpts(args[i].substring(1), "100") == 1)
usage();
} else {
doneOne = true;
process(args[i]);
}
}
if (!doneOne) {
process(System.in, "http://example.org/stdin", "standard input");
}
if ( startMem != -1) {
rt.gc();
System.out.println(rt.totalMemory()-rt.freeMemory()-startMem);
rt.gc();
System.out.println(rt.totalMemory()-rt.freeMemory()-startMem);
rt.gc();
System.out.println(rt.totalMemory()-rt.freeMemory()-startMem);
rt.gc();
System.out.println(rt.totalMemory()-rt.freeMemory()-startMem);
}
}
/**
* @param b false for quiet.
*/
private static StatementHandler getSH(boolean b) {
StatementHandler rslt = b?(StatementHandler)new SH(System.out):new NoSH();
if (andMeToo!=null)
rslt = new TwoSH(rslt,andMeToo);
return rslt;
}
static private void lineNumber() {
if (numbers) {
Locator locator = arp.getLocator();
if (locator != null)
print(
"# "
+ locator.getSystemId()
+ ":"
+ locator.getLineNumber()
+ "("
+ locator.getColumnNumber()
+ ")\n");
}
}
/*
* Options:
* -x Lax, Warnings suppressed
* -s Strict, Warnings are errors
* -f All errors are fatal.
* -u Suppress unqualified attribute warnings
* -t Error checking only, no n-triple output
* -b: set xml:base (same for all files?)
* -e: convert numbered warnings to errors
* -i: suppress numbered warnings
* -w: convert numbered errors/suppressed warnings to warnings
* -n: give line numbers
*
*/
static void usage() {
System.err.println(
"java <class-path> "
+ NTriple.class.getName()
+ " ( [ -[xstfurR]][ -b xmlBase -[eiw] NNN[,NNN...] ] [ file ] [ url ] )... ");
System.err.println(
" All options, files and URLs can be intemingled in any order.");
System.err.println(" They are processed from left-to-right.");
System.err.println(
" file Converts RDF/XML file into N-triples");
System.err.println(
" url Converts RDF/XML from URL into N-triples");
System.err.println(" -b uri Sets XML Base to the absolute URI.");
System.err.println(
" -r Content is RDF (default, no embedding, rdf:RDF tag may be omitted).");
System.err.println(
" -R RDF embedded in XML document, search for obligatory rdf:RDF start element.");
System.err.println(
" -t No n-triple output, error checking only.");
System.err.println(" -x Lax mode - warnings are suppressed.");
System.err.println(
" -s Strict mode - most warnings are errors.");
System.err.println(" -n Show line and column numbers.");
System.err.println(
" -u Allow unqualified attributes (defaults to warning).");
System.err.println(
" -f All errors are fatal - report first one only.");
System.err.println(" -b url Sets XML Base to the absolute url.");
System.err.println(" -e NNN[,NNN...]");
System.err.println(
" Treats numbered warning conditions as errrors.");
System.err.println(" -w NNN[,NNN...]");
System.err.println(
" Treats numbered error conditions as warnings.");
System.err.println(" -i NNN[,NNN...]");
System.err.println(
" Ignores numbered error/warning conditions.");
System.exit(1);
}
static final private Runtime rt = Runtime.getRuntime();
static private int startMem = -1;
static private int processOpts(String opts, String nextArg) {
boolean usedNext = false;
ARPOptions options = arp.getOptions();
for (int i = 0; i < opts.length(); i++) {
char opt = opts.charAt(i);
if ("beiwD".indexOf(opt) != -1) {
if (usedNext)
usage();
usedNext = true;
}
switch (opt) {
case 'D':
final int nStatements = Integer.parseInt(nextArg);
rt.gc(); rt.gc();
startMem = (int)(rt.totalMemory()-rt.freeMemory());
arp.getHandlers().setStatementHandler(new StatementHandler(){
int debugC = 0;
public void statement(AResource subj, AResource pred, AResource obj) {
statement(null,null,(ALiteral)null);
}
public void statement(AResource subj, AResource pred, ALiteral lit) {
if (++debugC%100 == 0) {
System.out.println("T: " + debugC);
rt.gc();
System.out.println("M1: "+ (rt.totalMemory()-rt.freeMemory()-startMem));
rt.gc();
System.out.println("M2: " + (rt.totalMemory()-rt.freeMemory()-startMem));
}
if ( debugC == 1 ){
rt.gc(); rt.gc();
startMem = (int)(rt.totalMemory()-rt.freeMemory());
}
if (debugC == nStatements) {
rt.gc();
System.err.println("Kill me now.");
try {
Thread.sleep(200000);
}
catch (Exception e){
// ignore
}
}
}
});
break;
case 'x' :
options.setLaxErrorMode();
break;
case 's' :
options.setStrictErrorMode();
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -