📄 n3jenawriterpp.java
字号:
{
out.println();
if (N3JenaWriter.DEBUG)
out.println("# One ref");
// Don't allow further one ref objects to be inlined.
allowDeep = false;
writeOneGraphNode((Resource) leftOverIter.next());
allowDeep = true;
}
// Are there any unattached RDF lists?
// We missed these earlier (assumed all DAML lists are values of some statement)
for (Iterator leftOverIter = rdfLists.iterator(); leftOverIter.hasNext();)
{
Resource r = (Resource) leftOverIter.next();
if (rdfListsDone.contains(r))
continue;
out.println();
if (N3JenaWriter.DEBUG)
out.println("# RDF List");
if (!r.isAnon() || countArcsTo(r) > 0 )
{
// Name it.
out.print(formatResource(r));
out.print(" :- ");
}
writeList(r);
out.println(" .");
}
//out.println() ;
//writeModelSimple(model, bNodesMap, base) ;
out.flush();
clearDatastructures() ;
}
// Need to decide between one line or many.
// Very hard to do a pretty thing here because the objects may be large or small or a mix.
protected void writeObjectList(Resource resource, Property property)
{
// if ( ! doObjectListsAsLists )
// {
// super.writeObjectList(resource, property) ;
// return ;
// }
String propStr = formatProperty(property);
// Find which objects are simple (i.e. not nested structures)
StmtIterator sIter = resource.listProperties(property);
Set simple = new HashSet() ;
Set complex = new HashSet() ;
for (; sIter.hasNext();)
{
Statement stmt = sIter.nextStatement();
RDFNode obj = stmt.getObject() ;
if ( isSimpleObject(obj) )
simple.add(obj) ;
else
complex.add(obj) ;
}
sIter.close() ;
// DEBUG
int simpleSize = simple.size() ;
int complexSize = complex.size() ;
// Write property/simple objects
if ( simple.size() > 0 )
{
String padSp = null ;
// Simple objects - allow property to be long and alignment to be lost
if ((propStr.length()+minGap) <= widePropertyLen)
padSp = pad(calcPropertyPadding(propStr)) ;
if ( doObjectListsAsLists )
{
// Write all simple objects as one list.
out.print(propStr);
out.incIndent(indentObject) ;
if ( padSp != null )
out.print(padSp) ;
else
out.println() ;
for (Iterator iter = simple.iterator(); iter.hasNext();)
{
RDFNode n = (RDFNode) iter.next();
writeObject(n);
// As an object list
if (iter.hasNext())
out.print(objectListSep);
}
out.decIndent(indentObject) ;
}
else
{
for (Iterator iter = simple.iterator(); iter.hasNext();)
{
// This is also the same as the complex case
// except the width the property can go in is different.
out.print(propStr);
out.incIndent(indentObject) ;
if ( padSp != null )
out.print(padSp) ;
else
out.println() ;
RDFNode n = (RDFNode) iter.next();
writeObject(n);
out.decIndent(indentObject) ;
// As an object list
if (iter.hasNext())
out.println(" ;");
}
}
}
// Now do complex objects.
// Write property each time for a complex object.
// Do not allow over long properties but same line objects.
if (complex.size() > 0)
{
// Finish the simple list if there was one
if ( simple.size() > 0 )
out.println(" ;");
int padding = -1 ;
String padSp = null ;
// Can we fit teh start of teh complex object on this line?
// DEBUG variable.
int tmp = propStr.length() ;
// Complex objects - do not allow property to be long and alignment to be lost
if ((propStr.length()+minGap) <= propertyCol)
{
padding = calcPropertyPadding(propStr) ;
padSp = pad(padding) ;
}
for (Iterator iter = complex.iterator(); iter.hasNext();)
{
int thisIndent = indentObject ;
//if ( i )
out.incIndent(thisIndent);
out.print(propStr);
if ( padSp != null )
out.print(padSp) ;
else
out.println() ;
RDFNode n = (RDFNode) iter.next();
writeObject(n);
out.decIndent(thisIndent);
if ( iter.hasNext() )
out.println(" ;");
}
}
return;
}
private boolean isSimpleObject(RDFNode node)
{
if (node instanceof Literal)
return true ;
Resource rObj = (Resource) node;
if ( allowDeep && oneRefObjects.contains(rObj) )
return false ;
return true ;
}
protected void writeObject(RDFNode node)
{
if (node instanceof Literal)
{
writeLiteral((Literal) node);
return;
}
Resource rObj = (Resource) node;
if ( allowDeep && ! isSimpleObject(rObj))
{
oneRefDone.add(rObj);
//int oldIndent = out.getIndent();
//out.setIndent(out.getCol());
//out.incIndent(4);
//out.println();
out.print("[ ");
out.incIndent(2);
writePropertiesForSubject(rObj);
out.decIndent(2);
out.println() ;
// Line up []
out.print("]");
//out.decIndent(4);
//out.setIndent(oldIndent);
return ;
}
if (rdfLists.contains(rObj))
if (countArcsTo(rObj) <= 1)
{
writeList(rObj);
return;
}
out.print(formatResource(rObj));
}
// Need to out.print in short (all on one line) and long forms (multiple lines)
// That needs starts point depth tracking.
private void writeList(Resource resource)
{
out.print( "(");
out.incIndent(2) ;
boolean listFirst = true;
for (Iterator iter = rdfListIterator(resource); iter.hasNext();)
{
if (!listFirst)
out.print( " ");
listFirst = false;
RDFNode n = (RDFNode) iter.next();
writeObject(n) ;
}
out.print( ")");
out.decIndent(2) ;
rdfListsDone.add(resource);
}
// Called before each writing run.
protected void allocateDatastructures()
{
rdfLists = new HashSet() ;
rdfListsAll = new HashSet() ;
rdfListsDone = new HashSet() ;
oneRefObjects = new HashSet() ;
oneRefDone = new HashSet() ;
}
// Especially release large intermediate memory objects
protected void clearDatastructures()
{
rdfLists = null ;
rdfListsAll = null ;
rdfListsDone = null ;
oneRefObjects = null ;
oneRefDone = null ;
}
}
/*
* (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.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -