KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > xmloutput > impl > BaseXMLWriter


1 /*
2  * (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  * [See end of file]
5  * $Id: BaseXMLWriter.java,v 1.46 2005/04/17 19:11:17 jeremy_carroll Exp $
6 */

7
8 package com.hp.hpl.jena.xmloutput.impl;
9
10 import com.hp.hpl.jena.xmloutput.RDFXMLWriterI;
11 import com.hp.hpl.jena.rdf.model.impl.*;
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.util.CharEncoding;
14 import com.hp.hpl.jena.util.FileUtils;
15 import com.hp.hpl.jena.rdf.model.impl.Util;
16 import com.hp.hpl.jena.JenaRuntime ;
17
18 import com.hp.hpl.jena.vocabulary.*;
19 import com.hp.hpl.jena.shared.*;
20
21 import com.hp.hpl.jena.rdf.arp.URI;
22 import com.hp.hpl.jena.rdf.arp.ARP;
23 import com.hp.hpl.jena.rdf.arp.MalformedURIException;
24
25 import java.io.*;
26 import java.util.*;
27 import java.nio.charset.Charset JavaDoc;
28
29 import org.apache.xerces.util.*;
30 import org.apache.oro.text.regex.*;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * This is not part of the public API.
36  * Base class for XML serializers.
37  * All methods with side-effects should be synchronized in this class and its
38  * subclasses. (i. e. XMLWriters assume that the world is not changing around
39  * them while they are writing).
40  *
41  * Functionality:
42  *
43  * <ul>
44  * <li>setProperty etc
45  * <li>namespace prefixes
46  * <li>xmlbase
47  * <li>relative URIs
48  * <li>encoding issues
49  * <li>anonymous node presentationall
50  * <li>errorHandler
51  * </ul>
52  *
53  * @author jjcnee
54  * @version Release='$Name: $' Revision='$Revision: 1.46 $' Date='$Date: 2005/04/17 19:11:17 $'
55 */

56 abstract public class BaseXMLWriter implements RDFXMLWriterI {
57     
58     /**
59          Introduced to cope with bug 832682: double spacing on windows platforms
60     */

61     private static final String JavaDoc newline_XMLNS =
62         System.getProperty( "line.separator" ) + " xmlns";
63     
64     public BaseXMLWriter() {
65         setupMaps();
66     }
67     
68     private static Log xlogger = LogFactory.getLog( BaseXMLWriter.class );
69   protected static SimpleLogger logger = new SimpleLogger() {
70     public void warn(String JavaDoc s) {
71         xlogger.warn(s);
72     }
73     public void warn(String JavaDoc s, Exception JavaDoc e) {
74         xlogger.warn(s,e);
75     }
76   };
77   
78   public static SimpleLogger setLogger(SimpleLogger lg) {
79     SimpleLogger old = logger;
80     logger= lg;
81     return old;
82   }
83   
84     abstract void unblockAll();
85     
86     abstract void blockRule(Resource r);
87
88     abstract void writeBody
89         ( Model mdl, PrintWriter pw, String JavaDoc baseURI, boolean inclXMLBase );
90                 
91     
92     private String JavaDoc attributeQuoteChar ="\"";
93     
94     protected String JavaDoc q(String JavaDoc s) {
95         return attributeQuoteChar +s + attributeQuoteChar;
96     }
97     
98     protected String JavaDoc qq(String JavaDoc s) {
99         return q(Util.substituteStandardEntities(s));
100     }
101
102     static private Set badRDF = new HashSet();
103     
104     /**
105         Counter used for allocating Jena transient namespace declarations.
106     */

107     private int jenaPrefixCount;
108     
109     static String JavaDoc RDFNS = RDF.getURI();
110     
111     static private Perl5Matcher matcher = new Perl5Matcher();
112     
113     static private Pattern jenaNamespace;
114     
115     static {
116         try {
117             jenaNamespace =
118                 new Perl5Compiler().compile("j\\.([1-9][0-9]*|cook\\.up)");
119         } catch (MalformedPatternException e) {
120         }
121         badRDF.add("RDF");
122         badRDF.add("Description");
123         badRDF.add("li");
124         badRDF.add("about");
125         badRDF.add("aboutEach");
126         badRDF.add("aboutEachPrefix");
127         badRDF.add("ID");
128         badRDF.add("nodeID");
129         badRDF.add("parseType");
130         badRDF.add("datatype");
131         badRDF.add("bagID");
132         badRDF.add("resource");
133     }
134
135     String JavaDoc xmlBase = null;
136
137     private URI baseURI;
138         
139     boolean longId = false;
140     
141     private boolean demandGoodURIs = true;
142     
143     int tab = 2;
144     
145     int width = 60;
146
147     HashMap anonMap = new HashMap();
148     
149     int anonCount = 0;
150     
151     static private RDFDefaultErrorHandler defaultErrorHandler =
152         new RDFDefaultErrorHandler();
153         
154     RDFErrorHandler errorHandler = defaultErrorHandler;
155
156     Boolean JavaDoc showXmlDeclaration = null;
157
158     /*
159      * There are two sorts of id's for anonymous resources. Short id's are the
160      * default, but require a mapping table. The mapping table means that
161      * serializing a large model could run out of memory. Long id's require no
162      * mapping table, but are less readable.
163      */

164
165     String JavaDoc anonId(Resource r) {
166         return longId ? longAnonId( r ) : shortAnonId( r );
167     }
168
169     /*
170      * A shortAnonId is computed by maintaining a mapping table from the internal
171      * id's of anon resources. The short id is the index into the table of the
172      * internal id.
173      */

174     private String JavaDoc shortAnonId(Resource r) {
175         String JavaDoc result = (String JavaDoc) anonMap.get(r.getId());
176         if (result == null) {
177             result = "A" + Integer.toString(anonCount++);
178             anonMap.put(r.getId(), result);
179         }
180         return result;
181     }
182
183     /*
184      * A longAnonId is the internal id of the anon resource expressed as a
185      * character string.
186      *
187      * This code makes no assumptions about the characters used in the
188      * implementation of an anon id. It checks if they are valid namechar
189      * characters and escapes the id if not.
190      */

191
192     private String JavaDoc longAnonId(Resource r) {
193         String JavaDoc rid = r.getId().toString();
194         return XMLChar.isValidNCName( rid ) ? rid : escapedId( rid );
195     }
196
197     /**
198         true means all namespaces defined in the model prefixes will be noted in xmlns
199         declarations; false means only "required" ones will be noted. Hook for configuration.
200     */

201     private boolean writingAllModelPrefixNamespaces = true;
202         
203     private Relation nameSpaces = new Relation();
204     
205     private Map ns;
206         
207     private Set namespacesNeeded;
208     
209     void addNameSpace(String JavaDoc uri) {
210         namespacesNeeded.add(uri);
211     }
212
213     boolean isDefaultNamespace( String JavaDoc uri ) {
214         return "".equals( ns.get( uri ) );
215     }
216         
217     private void addNameSpaces( Model model ) {
218         NsIterator nsIter = model.listNameSpaces();
219         while (nsIter.hasNext()) this.addNameSpace( nsIter.nextNs() );
220     }
221     
222     private void primeNamespace( Model model )
223         {
224         Map m = model.getNsPrefixMap();
225         Iterator it = m.entrySet().iterator();
226         while (it.hasNext())
227             {
228             Map.Entry e = (Map.Entry) it.next();
229             String JavaDoc key = (String JavaDoc) e.getKey();
230             String JavaDoc value = (String JavaDoc) e.getValue();
231             String JavaDoc already = this.getPrefixFor( value );
232             if (already == null)
233                 { this.setNsPrefix( model.getNsURIPrefix( value ), value );
234                 if (writingAllModelPrefixNamespaces) this.addNameSpace( value ); }
235             }
236         }
237
238     void setupMaps() {
239         nameSpaces.set11(RDF.getURI(), "rdf");
240         nameSpaces.set11(RDFS.getURI(), "rdfs");
241         nameSpaces.set11(DC.getURI(), "dc");
242         nameSpaces.set11(RSS.getURI(), "rss");
243         nameSpaces.set11("http://www.daml.org/2001/03/daml+oil.daml#", "daml");
244         nameSpaces.set11(VCARD.getURI(), "vcard");
245         nameSpaces.set11("http://www.w3.org/2002/07/owl#", "owl");
246     }
247                 
248     void workOutNamespaces() {
249         if (ns == null) {
250             ns = new HashMap();
251             Set prefixesUsed = new HashSet();
252             setFromWriterSystemProperties( ns, prefixesUsed );
253             setFromGivenNamespaces( ns, prefixesUsed );
254         }
255     }
256
257     private void setFromWriterSystemProperties( Map ns, Set prefixesUsed ) {
258         Iterator it = namespacesNeeded.iterator();
259         while (it.hasNext()) {
260             String JavaDoc uri = (String JavaDoc) it.next();
261             String JavaDoc val = JenaRuntime.getSystemProperty( RDFWriter.NSPREFIXPROPBASE + uri );
262             if (val != null && checkLegalPrefix( val ) && !prefixesUsed.contains( val )) {
263                 ns.put(uri, val);
264                 prefixesUsed.add(val);
265             }
266         }
267     }
268
269     private void setFromGivenNamespaces( Map ns, Set prefixesUsed ) {
270         Iterator it = namespacesNeeded.iterator();
271         while (it.hasNext()) {
272             String JavaDoc uri = (String JavaDoc) it.next();
273             if (ns.containsKey(uri))
274                 continue;
275             String JavaDoc val = null;
276             Set s = nameSpaces.forward(uri);
277             if (s != null) {
278                 Iterator it2 = s.iterator();
279                 if (it2.hasNext())
280                     val = (String JavaDoc) it2.next();
281                 if (prefixesUsed.contains(val))
282                     val = null;
283             }
284             if (val == null) {
285                 // just in case the prefix has already been used, look for a free one.
286
// (the usual source of such prefixes is reading in a model we wrote out earlier)
287
do { val = "j." + (jenaPrefixCount++); } while (prefixesUsed.contains( val ));
288             }
289             ns.put(uri, val);
290             prefixesUsed.add(val);
291         }
292     }
293
294     final synchronized public void setNsPrefix(String JavaDoc prefix, String JavaDoc ns) {
295         if (checkLegalPrefix(prefix)) {
296             nameSpaces.set11(ns, prefix);
297         }
298     }
299     
300     final public String JavaDoc getPrefixFor( String JavaDoc uri )
301         {
302         Set s = nameSpaces.backward( uri );
303         if (s != null && s.size() == 1) return (String JavaDoc) s.iterator().next();
304         return null;
305         }
306
307     String JavaDoc xmlnsDecl() {
308         workOutNamespaces();
309         StringBuffer JavaDoc rslt = new StringBuffer JavaDoc();
310         Iterator it = ns.entrySet().iterator();
311         while (it.hasNext()) {
312             Map.Entry ent = (Map.Entry) it.next();
313             String JavaDoc prefix = (String JavaDoc) ent.getValue();
314             String JavaDoc uri = (String JavaDoc) ent.getKey();
315             rslt.append( newline_XMLNS );
316             if (prefix.length() > 0) rslt.append( ':' ).append( prefix );
317             rslt.append( '=' ).append( qq( checkURI( uri ) ) );
318         }
319         return rslt.toString();
320     }
321
322     static final private int FAST = 1;
323     static final private int START = 2;
324     static final private int END = 3;
325     static final private int ATTR = 4;
326     static final private int FASTATTR = 5;
327     
328     String JavaDoc rdfEl(String JavaDoc local) {
329         return tag(RDFNS, local, FAST, true);
330     }
331     
332     String JavaDoc startElementTag(String JavaDoc uri, String JavaDoc local) {
333         return tag(uri, local, START, false);
334     }
335     
336     protected String JavaDoc startElementTag(String JavaDoc uriref) {
337         return splitTag(uriref, START);
338     }
339     
340     String JavaDoc attributeTag(String JavaDoc uriref) {
341         return splitTag(uriref, ATTR);
342     }
343     
344     String JavaDoc attributeTag(String JavaDoc uri, String JavaDoc local) {
345         return tag(uri, local, ATTR, false);
346     }
347     
348     String JavaDoc rdfAt(String JavaDoc local) {
349         return tag(RDFNS, local, FASTATTR, true);
350     }
351     
352     String JavaDoc endElementTag(String JavaDoc uri, String JavaDoc local) {
353         return tag(uri, local, END, false);
354     }
355     
356     protected String JavaDoc endElementTag(String JavaDoc uriref) {
357         return splitTag(uriref, END);
358     }
359     
360     String JavaDoc splitTag(String JavaDoc uriref, int type) {
361         int split = Util.splitNamespace( uriref );
362         if (split == uriref.length()) throw new InvalidPropertyURIException( uriref );
363         return tag( uriref.substring( 0, split ), uriref.substring( split ), type, true );
364     }
365     
366     static public boolean dbg = false;
367     
368     String JavaDoc tag( String JavaDoc namespace, String JavaDoc local, int type, boolean localIsQname) {
369         if (dbg)
370             System.err.println(namespace + " - " + local);
371         String JavaDoc prefix = (String JavaDoc) ns.get( namespace );
372         if (type != FAST && type != FASTATTR) {
373             if ((!localIsQname) && !XMLChar.isValidNCName(local))
374                 return splitTag(namespace + local, type);
375             if (namespace.equals(RDFNS)) {
376                 // Description, ID, nodeID, about, aboutEach, aboutEachPrefix, li
377
// bagID parseType resource datatype RDF
378
if (badRDF.contains(local)) {
379                     logger.warn( "The URI rdf:" + local + " cannot be serialized in RDF/XML." );
380                     throw new InvalidPropertyURIException( "rdf:" + local );
381                 }
382             }
383         }
384         boolean cookUp = false;
385         if (prefix == null) {
386             checkURI( namespace );
387             logger.warn(
388                 "Internal error: unexpected QName URI: <"
389                     + namespace
390                     + ">. Fixing up with j.cook.up code.",
391                 new BrokenException( "unexpected QName URI " + namespace ));
392             cookUp = true;
393         } else if (prefix.length() == 0) {
394             if (type == ATTR || type == FASTATTR)
395                 cookUp = true;
396             else
397                 return local;
398         }
399         if (cookUp) return cookUpAttribution( type, namespace, local );
400         return prefix + ":" + local;
401     }
402     
403     private String JavaDoc cookUpAttribution( int type, String JavaDoc namespace, String JavaDoc local )
404         {
405         String JavaDoc prefix = "j.cook.up";
406         switch (type) {
407             case FASTATTR :
408             case ATTR :
409                 return "xmlns:" + prefix + "=" + qq( namespace ) + " " + prefix + ":" + local;
410             case START :
411                 return prefix + ":" + local + " xmlns:" + prefix+ "=" + qq( namespace );
412             default:
413             case END :
414                 return prefix + ":" + local;
415             case FAST :
416               // logger.fatal("Unreachable code - reached.");
417
throw new BrokenException( "cookup reached final FAST" );
418             }
419         }
420
421     /** Write out an XML serialization of a model.
422      * @param model the model to be serialized
423      * @param out the OutputStream to receive the serialization
424      * @param base The URL at which the file will be placed.
425      * @throws IOException if an io error occurs
426      */

427     final public void write(Model model, OutputStream out, String JavaDoc base)
428          { write(model, FileUtils.asUTF8(out), base); }
429
430     /** Serialize Model <code>model</code> to Writer <code>out</out>.
431      * @param out The Writer to which the serialization should be sent.
432      * @param model The model to be written.
433      * @param base the base URI for relative URI calculations. <code>
434      * null</code> means use only absolute URI's.
435      */

436     synchronized public void write(Model baseModel, Writer out, String JavaDoc base)
437          {
438         Model model = ModelFactory.withHiddenStatements( baseModel );
439         this.namespacesNeeded = new HashSet();
440         this.ns = null;
441         primeNamespace( baseModel );
442         addNameSpace(RDF.getURI());
443         addNameSpaces(model);
444         jenaPrefixCount = 0;
445         PrintWriter pw = out instanceof PrintWriter ? (PrintWriter) out : new PrintWriter( out );
446         if (!Boolean.FALSE.equals(showXmlDeclaration)) writeXMLDeclaration( out, pw );
447         writeXMLBody( model, pw, base );
448         pw.flush();
449     }
450     
451     private void writeXMLBody( Model model, PrintWriter pw, String JavaDoc base ) {
452         try {
453             if (xmlBase == null) {
454                 baseURI = (base == null || base.length() == 0) ? null : new URI(base);
455                 writeBody(model, pw, base, false);
456             } else {
457                 baseURI = xmlBase.length() == 0 ? null : new URI(xmlBase);
458                 writeBody(model, pw, xmlBase, true);
459             }
460         } catch (MalformedURIException e) {
461             throw new BadURIException( e.getMessage(), e);
462         }
463     }
464
465     private void writeXMLDeclaration(Writer out, PrintWriter pw) {
466         String JavaDoc decl = null;
467         if (out instanceof OutputStreamWriter) {
468             String JavaDoc javaEnc = ((OutputStreamWriter) out).getEncoding();
469             // System.err.println(javaEnc);
470
if (!(javaEnc.equals("UTF8") || javaEnc.equals("UTF-16"))) {
471                 CharEncoding encodingInfo = CharEncoding.create(javaEnc);
472                 
473                 String JavaDoc ianaEnc = encodingInfo.name();
474                 decl = "<?xml version="+q("1.0")+" encoding=" + q(ianaEnc) + "?>";
475                 if (!encodingInfo.isIANA())
476                  logger.warn(encodingInfo.warningMessage()+"\n"+
477                             " It is better to use a FileOutputStream, in place of a FileWriter.");
478                        
479             }
480         }
481         if (decl == null && showXmlDeclaration != null)
482             decl = "<?xml version="+q("1.0")+"?>";
483         if (decl != null) {
484             pw.println(decl);
485         }
486     }
487
488     /** Set an error handler.
489      * @param errHandler The new error handler to be used, or null for the default handler.
490      * @return the old error handler
491      */

492     synchronized public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler) {
493         // null means no user defined error handler.
494
// We implement this using defaultErrorHandler,
495
// but hide this fact from the user.
496
RDFErrorHandler rslt = errorHandler;
497         if (rslt == defaultErrorHandler) rslt = null;
498         errorHandler = errHandler == null ? defaultErrorHandler : errHandler;
499         return rslt;
500     }
501
502     static private final char ESCAPE = 'X';
503     
504     static private String JavaDoc escapedId(String JavaDoc id) {
505         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
506         for (int i = 0; i < id.length(); i++) {
507             char ch = id.charAt(i);
508             if (ch != ESCAPE
509                 && (i == 0 ? XMLChar.isNCNameStart(ch) : XMLChar.isNCName(ch))) {
510                 result.append( ch );
511             } else {
512                 escape( result, ch );
513             }
514         }
515         return result.toString();
516     }
517
518     static final char [] hexchar = "0123456789abcdef".toCharArray();
519                 
520     static private void escape( StringBuffer JavaDoc sb, char ch) {
521         sb.append( ESCAPE );
522         int charcode = ch;
523         do {
524             sb.append( hexchar[charcode & 15] );
525             charcode = charcode >> 4;
526         } while (charcode != 0);
527         sb.append( ESCAPE );
528     }
529     
530     /**
531         Set the writer property propName to the value obtained from propValue. Return an
532         Object representation of the original value.
533          
534         @see com.hp.hpl.jena.rdf.model.RDFWriter#setProperty(java.lang.String, java.lang.Object)
535      */

536     final synchronized public Object JavaDoc setProperty( String JavaDoc propName, Object JavaDoc propValue ) {
537         if (propName.equalsIgnoreCase("showXmlDeclaration")) {
538             return setShowXmlDeclaration(propValue);
539         } else if (propName.equalsIgnoreCase( "minimalPrefixes" )) {
540             try { return new Boolean JavaDoc( !writingAllModelPrefixNamespaces ); }
541             finally { writingAllModelPrefixNamespaces = !getBoolean( propValue ); }
542         } else if (propName.equalsIgnoreCase("xmlbase")) {
543             String JavaDoc result = xmlBase;
544             xmlBase = (String JavaDoc) propValue;
545             return result;
546         } else if (propName.equalsIgnoreCase("tab")) {
547             return setTab( propValue );
548         } else if (propName.equalsIgnoreCase("width")) {
549             return setWidth(propValue);
550         } else if (propName.equalsIgnoreCase("longid")) {
551             Boolean JavaDoc result = new Boolean JavaDoc(longId);
552             longId = getBoolean(propValue);
553             return result;
554         } else if (propName.equalsIgnoreCase("attributeQuoteChar")) {
555             return setAttributeQuoteChar(propValue);
556         } else if (propName.equalsIgnoreCase( "allowBadURIs" )) {
557             Boolean JavaDoc result = new Boolean JavaDoc( !demandGoodURIs );
558             demandGoodURIs = !getBoolean(propValue);
559             return result;
560         } else if (propName.equalsIgnoreCase("prettyTypes")) {
561             return setTypes((Resource[]) propValue);
562         } else if (propName.equalsIgnoreCase("relativeURIs")) {
563             int old = relativeFlags;
564             relativeFlags = str2flags((String JavaDoc) propValue);
565             return flags2str(old);
566         } else if (propName.equalsIgnoreCase("blockRules")) {
567             return setBlockRules(propValue);
568         } else {
569             logger.warn("Unsupported property: " + propName);
570             return null;
571         }
572     }
573     
574     private String JavaDoc setAttributeQuoteChar(Object JavaDoc propValue) {
575         String JavaDoc result = attributeQuoteChar;
576         if ( "\"".equals(propValue) || "'".equals(propValue) )
577           attributeQuoteChar = (String JavaDoc)propValue;
578         else
579           logger.warn("attributeQutpeChar must be either \"\\\"\" or \', not \""+propValue+"\"" );
580         return result;
581     }
582
583     private Integer JavaDoc setWidth(Object JavaDoc propValue) {
584         Integer JavaDoc result = new Integer JavaDoc(width);
585         if (propValue instanceof Integer JavaDoc) {
586             width = ((Integer JavaDoc) propValue).intValue();
587         } else {
588             try {
589                 width = Integer.parseInt((String JavaDoc) propValue);
590             } catch (Exception JavaDoc e) {
591                 logger.warn( "Bad value for width: '" + propValue + "' [" + e.getMessage() + "]" );
592             }
593         }
594         return result;
595     }
596
597     private Integer JavaDoc setTab(Object JavaDoc propValue) {
598         Integer JavaDoc result = new Integer JavaDoc(tab);
599         if (propValue instanceof Integer JavaDoc) {
600             tab = ((Integer JavaDoc) propValue).intValue();
601         } else {
602             try {
603                 tab = Integer.parseInt((String JavaDoc) propValue);
604             } catch (Exception JavaDoc e) {
605                 logger.warn( "Bad value for tab: '" + propValue + "' [" + e.getMessage() + "]" );
606             }
607         }
608         return result;
609     }
610
611     private String JavaDoc setShowXmlDeclaration(Object JavaDoc propValue) {
612         String JavaDoc oldValue;
613         if (showXmlDeclaration == null)
614             oldValue = null;
615         else
616             oldValue = showXmlDeclaration.toString();
617         if (propValue == null)
618             showXmlDeclaration = null;
619         else if (propValue instanceof Boolean JavaDoc)
620             showXmlDeclaration = (Boolean JavaDoc) propValue;
621         else if (propValue instanceof String JavaDoc) {
622             String JavaDoc propValueStr = (String JavaDoc) propValue;
623             if (propValueStr.equalsIgnoreCase("default")) {
624                 showXmlDeclaration = null;
625             }
626             if (propValueStr.equalsIgnoreCase("true"))
627                 showXmlDeclaration = Boolean.TRUE;
628             else if (propValueStr.equalsIgnoreCase("false"))
629                 showXmlDeclaration = Boolean.FALSE;
630             else
631                 // Also overloading the error condition.
632
throw new BadBooleanException( propValueStr );
633         }
634         return oldValue;
635     }
636
637     /**
638         Answer the boolean value corresponding to o, which must either be a Boolean,
639         or a String parsable as a Boolean.
640     */

641     static private boolean getBoolean( Object JavaDoc o ) {
642         if (o instanceof Boolean JavaDoc)
643             return ((Boolean JavaDoc) o).booleanValue();
644         else
645             return Boolean.valueOf((String JavaDoc) o).booleanValue();
646     }
647
648     Resource[] setTypes( Resource x[] ) {
649         logger.warn( "prettyTypes is not a property on the Basic RDF/XML writer." );
650         return null;
651     }
652     
653     private Resource blockedRules[] = new Resource[]{RDFSyntax.propertyAttr};
654     
655     Resource[] setBlockRules(Object JavaDoc o) {
656         Resource rslt[] = blockedRules;
657         unblockAll();
658         if (o instanceof Resource[]) {
659             blockedRules = (Resource[]) o;
660         } else {
661             StringTokenizer tkn = new StringTokenizer((String JavaDoc) o, ", ");
662             Vector v = new Vector();
663             while (tkn.hasMoreElements()) {
664                 String JavaDoc frag = tkn.nextToken();
665                 // System.err.println("Blocking " + frag);
666
if (frag.equals("daml:collection"))
667                     v.add(DAML_OIL.collection);
668                 else
669                     v.add(new ResourceImpl(RDFSyntax.getURI() + frag));
670             }
671
672             blockedRules = new Resource[v.size()];
673             v.copyInto(blockedRules);
674         }
675         for (int i = 0; i < blockedRules.length; i++)
676             blockRule(blockedRules[i]);
677         return rslt;
678     }
679     /*
680     private boolean sameDocument = true;
681     private boolean network = false;
682     private boolean absolute = true;
683     private boolean relative = true;
684     private boolean parent = true;
685     private boolean grandparent = false;
686     */

687     private int relativeFlags =
688         URI.SAMEDOCUMENT | URI.ABSOLUTE | URI.RELATIVE | URI.PARENT;
689
690     /**
691         Answer the form of the URI after relativisation according to the relativeFlags set
692         by properties. If the flags are 0 or the base URI is null, answer the original URI.
693         Throw an exception if the URI is "bad" and we demandGoodURIs.
694     */

695     protected String JavaDoc relativize( String JavaDoc uri ) {
696         return relativeFlags != 0 && baseURI != null
697             ? relativize( baseURI, uri )
698             : checkURI( uri );
699     }
700     
701     /**
702         Answer the relative form of the URI against the base, according to the relativeFlags.
703         This method exists only to encapsulate the checked MalformedURIException.
704     */

705     private String JavaDoc relativize( URI base, String JavaDoc uri ) {
706         try { return base.relativize( uri, relativeFlags); }
707         catch (MalformedURIException e) { throw new JenaException( e ); }
708         }
709
710     /**
711         Answer the argument URI, but if we demandGoodURIs and it isn't good, throw
712         a JenaException that encapsulates a MalformedURIException. There doesn't
713         appear to be a convenient URI.checkGood() kind of method, alas.
714      */

715     private String JavaDoc checkURI( String JavaDoc uri ) {
716         if (demandGoodURIs)
717             try { new URI( uri ); }
718             catch (MalformedURIException e) {
719                 throw new BadURIException( "Only well-formed absolute URIrefs can be included in RDF/XML output: " + uri, e );
720             }
721         return uri;
722     }
723     
724     /**
725         Answer true iff prefix is a "legal" prefix to use, ie, is empty [for the default namespace]
726         or an NCName that does not start with "xml" and does not match the reserved-to-Jena
727         pattern.
728     */

729     private boolean checkLegalPrefix( String JavaDoc prefix ) {
730         if (prefix.equals(""))
731             return true;
732         if (prefix.toLowerCase().startsWith( "xml" ))
733             logger.warn( "Namespace prefix '" + prefix + "' is reserved by XML." );
734         else if (!XMLChar.isValidNCName(prefix))
735             logger.warn( "'" + prefix + "' is not a legal namespace prefix." );
736         else if (matcher.matches(prefix, jenaNamespace))
737             logger.warn( "Namespace prefix '" + prefix + "' is reserved by Jena." );
738         else
739             return true;
740         return false;
741     }
742
743     static private String JavaDoc flags2str(int f) {
744     StringBuffer JavaDoc oldValue = new StringBuffer JavaDoc(64);
745     if ( (f&URI.SAMEDOCUMENT)!=0 )
746        oldValue.append( "same-document, " );
747     if ( (f&URI.NETWORK)!=0 )
748        oldValue.append( "network, ");
749     if ( (f&URI.ABSOLUTE)!=0 )
750        oldValue.append("absolute, ");
751     if ( (f&URI.RELATIVE)!=0 )
752        oldValue.append("relative, ");
753     if ((f&URI.PARENT)!=0)
754        oldValue.append("parent, ");
755     if ((f&URI.GRANDPARENT)!=0)
756        oldValue.append("grandparent, ");
757     if (oldValue.length() > 0)
758        oldValue.setLength(oldValue.length()-2);
759        return oldValue.toString();
760     }
761
762     public static int str2flags(String JavaDoc pv){
763     StringTokenizer tkn = new StringTokenizer(pv,", ");
764     int rslt = 0;
765     while ( tkn.hasMoreElements() ) {
766         String JavaDoc flag = tkn.nextToken();
767         if ( flag.equals("same-document") )
768            rslt |= URI.SAMEDOCUMENT;
769         else if ( flag.equals("network") )
770            rslt |= URI.NETWORK;
771         else if ( flag.equals("absolute") )
772            rslt |= URI.ABSOLUTE;
773         else if ( flag.equals("relative") )
774            rslt |= URI.RELATIVE;
775         else if ( flag.equals("parent") )
776            rslt |= URI.PARENT;
777         else if ( flag.equals("grandparent") )
778            rslt |= URI.GRANDPARENT;
779         else
780     
781         logger.warn(
782             "Incorrect property value for relativeURIs: " + flag
783             );
784     }
785     return rslt;
786     }
787     
788 }
789
790 /*
791     (c) Copyright 2000, 2001, 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
792     All rights reserved.
793
794     Redistribution and use in source and binary forms, with or without
795     modification, are permitted provided that the following conditions
796     are met:
797
798     1. Redistributions of source code must retain the above copyright
799        notice, this list of conditions and the following disclaimer.
800
801     2. Redistributions in binary form must reproduce the above copyright
802        notice, this list of conditions and the following disclaimer in the
803        documentation and/or other materials provided with the distribution.
804
805     3. The name of the author may not be used to endorse or promote products
806        derived from this software without specific prior written permission.
807
808     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
809     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
810     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
811     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
812     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
813     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
814     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
815     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
816     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
817     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
818 */
Popular Tags