001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: AssemblerException.java,v 1.7 2008/01/02 12:07:39 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.exceptions;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.assemblers.AssemblerGroup;
012: import com.hp.hpl.jena.rdf.model.*;
013: import com.hp.hpl.jena.shared.JenaException;
014: import com.hp.hpl.jena.vocabulary.RDFS;
015:
016: /**
017: Assembler Exception class: contains code shared by all the Assembler
018: exceptions.
019:
020: @author kers
021: */
022: public class AssemblerException extends JenaException {
023: protected final Resource root;
024: protected List doing = new ArrayList();
025:
026: public AssemblerException(Resource root, String string, Throwable t) {
027: super (string, t);
028: this .root = root;
029: }
030:
031: public AssemblerException(Resource root, String message) {
032: super (message);
033: this .root = root;
034: }
035:
036: /**
037: Answer the root object whose model-filling was aborted
038: */
039: public Resource getRoot() {
040: return root;
041: }
042:
043: /**
044: XXX
045: */
046: public AssemblerException pushDoing(AssemblerGroup.Frame frame) {
047: doing.add(frame);
048: return this ;
049: }
050:
051: /**
052: Answer a "nice" representation of <code>r</code>, suitable for appearance
053: within an exception message.
054: */
055: protected static String nice(Resource r) {
056: String rString = r.asNode().toString(r.getModel());
057: return r.isAnon() ? rString + getLabels(r) : rString;
058: }
059:
060: private static String getLabels(Resource r) {
061: Model m = r.getModel();
062: String labels = "", prefix = "labels: ";
063: for (StmtIterator it = r.listProperties(RDFS.label); it
064: .hasNext();) {
065: String label = it.nextStatement().getObject().asNode()
066: .toString(m, true);
067: labels += prefix + label;
068: prefix = ", ";
069: }
070: return labels.equals("") ? getIncomingProperty(r) : " ["
071: + labels + "]";
072: }
073:
074: private static String getIncomingProperty(Resource r) {
075: String incomings = "", prefix = "";
076: StmtIterator it = r.getModel().listStatements(null, null, r);
077: while (it.hasNext()) {
078: Statement s = it.nextStatement();
079: incomings += prefix + nice(s.getPredicate()) + " of "
080: + nice(s.getSubject());
081: prefix = ", ";
082: }
083: return incomings.equals("") ? "" : " [" + incomings + "]";
084: }
085:
086: protected static String nice(RDFNode r) {
087: return r.isLiteral() ? r.asNode().toString()
088: : nice((Resource) r);
089: }
090:
091: public List getDoing() {
092: return doing;
093: }
094:
095: public String toString() {
096: String parent = super .toString();
097: String frame = frameStrings();
098: return frame.equals("") ? parent : parent + "\n doing:\n"
099: + frame;
100: }
101:
102: protected String frameStrings() {
103: StringBuffer result = new StringBuffer();
104: for (Iterator it = doing.iterator(); it.hasNext();)
105: result.append(" ").append(it.next().toString()).append(
106: "\n");
107: return result.toString();
108: }
109: }
110:
111: /*
112: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
113: * All rights reserved.
114: *
115: * Redistribution and use in source and binary forms, with or without
116: * modification, are permitted provided that the following conditions
117: * are met:
118: * 1. Redistributions of source code must retain the above copyright
119: * notice, this list of conditions and the following disclaimer.
120: * 2. Redistributions in binary form must reproduce the above copyright
121: * notice, this list of conditions and the following disclaimer in the
122: * documentation and/or other materials provided with the distribution.
123: * 3. The name of the author may not be used to endorse or promote products
124: * derived from this software without specific prior written permission.
125: *
126: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
127: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
128: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
129: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
130: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
131: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
132: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
133: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
134: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
135: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
136: */
|