My favorites
▼
|
Sign in
lindenb
Pierre Lindenbaum's library
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
java
/
org
/
lindenb
/
sw
/
io
/
RDFHandler.java
‹r129
r508
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
package org.lindenb.sw.io;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.lindenb.sw.vocabulary.RDF;
import org.lindenb.util.Compilation;
import org.lindenb.util.Debug;
import org.lindenb.xml.XMLUtilities;
import static org.lindenb.util.Debug.debug;
/**
* RDFHandler
* RDF reader calling the method 'found' each time it finds a
* new RDFStatement
* @author Pierre Lindenbaum PhD plindenbaum@yahoo.fr
*
*/
public class RDFHandler
{
private static long ID_GENERATOR = System.currentTimeMillis();
private XMLInputFactory factory=null;
private XMLEventReader parser=null;
private URI base=null;
private static class RDFEvent
{
URI subject=null;
URI predicate=null;
Object value=null;
URI valueType=null;
String lang=null;
int listIndex=-1;
public void write(PrintWriter out)
{
out.println("<rdf:Description>");
out.println("\t<rdf:type rdf:resource=\""+ RDF.NS+"Statement\"/>");
if(subject.isAbsolute() ||
subject.toString().length()==0)
{
out.println("\t<rdf:subject rdf:resource=\""+
escapeXML(subject.toString()) +
"\"/>");
}
else
{
out.println("\t<rdf:subject rdf:nodeID=\""+
escapeXML(subject.toString()) +
"\"/>");
}
if(predicate.isAbsolute() ||
predicate.toString().length()==0)
{
out.println("\t<rdf:predicate rdf:resource=\""+
escapeXML(predicate.toString())
+"\"/>");
}
else
{
out.println("\t<rdf:predicate rdf:nodeID=\""+
escapeXML(predicate.toString()) +
"\"/>");
}
out.print("\t<rdf:object ");
if(value instanceof URI)
{
URI val=URI.class.cast(value);
if(val.isAbsolute() ||
val.toString().length()==0)
{
out.println("rdf:resource=\""+ escapeXML(val.toString()) +"\"/>");
}
else
{
out.println("rdf:nodeID=\""+ escapeXML(val.toString()) +"\"/>");
}
}
else
{
if(this.valueType!=null)
{
out.print("rdf:datatype=\""+
escapeXML(this.valueType.toString()) +
"\" ");
}
if(this.lang!=null)
{
out.print("xml:lang=\""+ this.lang +"\" ");
}
out.println(">"+ escapeXML(value.toString()) +"</rdf:object>");
}
out.println("</rdf:Description>");
}
@Override
public String toString()
{
StringWriter b= new StringWriter();
write(new PrintWriter(b,true));
return b.toString();
}
}
/**
* constructor
* initialize the XMLInputFactory
*/
public RDFHandler()
{
this.factory = XMLInputFactory.newInstance();
this.factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
this.factory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
this.factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
}
/**
* called when this handler finds a new RDF triple
* @param event
*/
protected void found(
RDFEvent event
) throws XMLStreamException
{
try{
found(event.subject,event.predicate,event.value,event.valueType,event.lang,event.listIndex);
} catch(IOException err)
{
throw new XMLStreamException(err);
}
}
/**
* This method should be overridden by the user
* @param subject
* @param predicate
* @param value
* @param dataType
* @param lang
* @param index
* @throws IOException
*/
public void found(
URI subject,
URI predicate,
Object value,
URI dataType,
String lang,
int index) throws IOException
{
}
public void setBase(URI base) {
this.base = base;
}
/** return XMLEventReader */
protected XMLEventReader getReader()
{
return this.parser;
}
/** do the parsing */
private void read(Reader in) throws XMLStreamException
{
try {
/** create a XML Event parser */
this.parser = this.factory.createXMLEventReader(in);
/** loop until we find a rdf:RDF element */
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isStartElement())
{
StartElement start=(StartElement)event;
debug("found start "+start);
if(name2string(start).equals(RDF.NS+"RDF"))
{
debug("found RDF");
parseRDF();
}
}
}
}
catch (URISyntaxException e)
{
this.parser=null;
throw new XMLStreamException(e);
}
this.parser=null;
}
private void parseRDF() throws XMLStreamException,URISyntaxException
{
debug("In RDF ROOT: loop over each rdf:Description");
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isEndElement())
{
return;
}
else if(event.isStartElement())
{
parseDescription(event.asStartElement());
}
else if(event.isProcessingInstruction())
{
throw new XMLStreamException("Found Processing Instruction in RDF ???");
}
else if(event.isCharacters() &&
event.asCharacters().getData().trim().length()>0)
{
throw new XMLStreamException("Found text in RDF ???");
}
}
}
/**
* Parse description of a Resource
* @param description
* @return
* @throws URISyntaxException
* @throws XMLStreamException
*/
private URI parseDescription(StartElement description) throws URISyntaxException,XMLStreamException
{
debug("Found a new rdf:Description "+description.getName());
URI descriptionURI=null;
Attribute att= description.getAttributeByName(new QName(RDF.NS,"about"));
if(att!=null) descriptionURI= createURI( att.getValue());
if(descriptionURI==null)
{
att= description.getAttributeByName(new QName(RDF.NS,"nodeID"));
if(att!=null) descriptionURI= createURI( att.getValue());
}
if(descriptionURI==null)
{
att= description.getAttributeByName(new QName(RDF.NS,"ID"));
if(att!=null) descriptionURI= resolveBase(att.getValue());
}
if(descriptionURI==null)
{
descriptionURI= createAnonymousURI();
}
debug("Description uri=\""+descriptionURI+"\"");
QName qn=description.getName();
if(!(qn.getNamespaceURI().equals(RDF.NS) &&
qn.getLocalPart().equals("Description")))
{
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= createURI(RDF.NS+"type");
evt.value=name2uri(qn);
found(evt);
}
/** loop over attributes */
for(Iterator<?> i=description.getAttributes();
i.hasNext();)
{
att=(Attribute)i.next();
qn= att.getName();
String local= qn.getLocalPart();
if(qn.getNamespaceURI().equals(RDF.NS) &&
( local.equals("about") ||
local.equals("ID") ||
local.equals("nodeID")))
{
continue;
}
debug("found Attribute;"+att);
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= name2uri(qn);
evt.value= att.getValue();
found(evt);
}
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isEndElement())
{
return descriptionURI;
}
else if(event.isStartElement())
{
parsePredicate(descriptionURI,event.asStartElement());
}
else if(event.isProcessingInstruction())
{
throw new XMLStreamException("Found Processing Instruction in RDF ???");
}
else if(event.isCharacters() &&
event.asCharacters().getData().trim().length()>0)
{
throw new XMLStreamException("Found text in RDF ??? \""+
event.asCharacters().getData()+"\""
);
}
}
return descriptionURI;
}
/**
* parse predicate
* @param descriptionURI
* @param predicate
* @throws URISyntaxException
* @throws XMLStreamException
*/
private void parsePredicate(URI descriptionURI,StartElement predicate) throws URISyntaxException,XMLStreamException
{
String parseType=null;
String lang=null;
URI datatype=null;
Attribute att;
QName qn=null;
URI resource=null;
URI predicateURI=name2uri(predicate.getName());
debug("parse rdf:description=\""+descriptionURI+"\" predicate:"+predicateURI);
/** collect attributes */
for(int loop=0;loop<2;++loop)
{
for(Iterator<?> i=predicate.getAttributes();
i.hasNext();)
{
att=(Attribute)i.next();
qn= att.getName();
String local= qn.getLocalPart();
if(qn.getPrefix().equals("xml") &&
local.equals("lang"))
{
if(loop==0) lang=att.getValue();
continue;
}
else if(qn.getNamespaceURI().equals(RDF.NS))
{
if(local.equals("parseType"))
{
if(loop==0) parseType=att.getValue();
debug("parseType:"+parseType);
continue;
}
else if(local.equals("datatype"))
{
if(loop==0) datatype= createURI(att.getValue());
debug("dataType="+datatype);
continue;
}
else if(local.equals("resource"))
{
if(loop==0) resource= createURI(att.getValue());
debug("rdf:resource="+resource);
continue;
}
else if(local.equals("nodeID"))
{
if(loop==0) resource= createURI(att.getValue());
debug("rdf:nodeID="+resource);
continue;
}
else if(local.equals("ID"))
{
if(loop==0) resource= resolveBase(att.getValue());
debug("ID="+resource);
continue;
}
}
if(loop==1)
{
if(resource!=null)
{
debug(resource);
RDFEvent evt= new RDFEvent();
evt.subject= resource;
evt.predicate= name2uri(att.getName());
evt.value=att.getValue();
found(evt);
}
else
{
throw new XMLStreamException("Cannot handle attribute "+att);
}
}
}
}
if(resource!=null)
{
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value=resource;
found(evt);
debug();
XMLEvent event=getReader().peek();
if(event!=null && event.isEndElement())
{
debug();
getReader().nextEvent();
return;
}
throw new XMLStreamException("Expected a EndElement for this element");
}
if(parseType==null) parseType="default";
if(parseType.equals("Literal"))
{
StringBuilder b= parseLiteral();
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value= b.toString();
evt.lang=lang;
evt.valueType=datatype;
found(evt);
}
else if(parseType.equals("Resource"))
{
URI blanck = createAnonymousURI();
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value=blanck;
evt.lang=lang;
evt.valueType=datatype;
found(evt);
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isStartElement())
{
parsePredicate(blanck, event.asStartElement());
}
else if(event.isEndElement())
{
return;
}
}
}
else if(parseType.equals("Collection"))
{
int index=0;
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isStartElement())
{
URI value= parseDescription(event.asStartElement());
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value=value;
evt.lang=lang;
evt.valueType=datatype;
evt.listIndex=(++index);
found(evt);
}
else if(event.isEndElement())
{
return;
}
}
}
else
{
boolean foundResourceAsChild=false;
StringBuilder b= new StringBuilder();
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isStartElement())
{
if(b.toString().trim().length()!=0)
{
throw new XMLStreamException(
"Bad text \""+b+"\" before "+
event.asStartElement().getName()
);
}
URI childURI=parseDescription(event.asStartElement());
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value= childURI;
found(evt);
b.setLength(0);
foundResourceAsChild=true;
}
else if(event.isCharacters())
{
b.append(event.asCharacters().getData());
}
else if(event.isEndElement())
{
if(!foundResourceAsChild)
{
RDFEvent evt= new RDFEvent();
evt.subject=descriptionURI;
evt.predicate= predicateURI;
evt.value= b.toString();
evt.lang=lang;
evt.valueType=datatype;
found(evt);
}
else
{
if(b.toString().trim().length()!=0) throw new XMLStreamException("Found bad text "+b);
}
return;
}
}
}
}
private URI resolveBase(String ID) throws URISyntaxException
{
if(this.base==null) return createURI(ID);
return this.base.resolve(ID);
}
private StringBuilder parseLiteral() throws XMLStreamException
{
StringBuilder b=new StringBuilder();
QName qn;
int depth=0;
while(getReader().hasNext())
{
XMLEvent event = getReader().nextEvent();
if(event.isCharacters())
{
b.append(escapeXML(event.asCharacters().getData()));
}
else if(event.isProcessingInstruction())
{
b.append("<?"+event.asCharacters()+"?>");
}
else if(event.isEndElement())
{
if(depth==0) return b;
qn= event.asEndElement().getName();
b.append("</"+qn.getPrefix()+":"+qn.getLocalPart()+">");
depth--;
}
else if(event.isStartElement())
{
qn= event.asEndElement().getName();
b.append("<"+qn.getPrefix()+":"+qn.getLocalPart());
for(Iterator<?> i=event.asStartElement().getAttributes();
i.hasNext();)
{
Attribute att=(Attribute)i.next();
qn= att.getName();
b.append(" ").
append(qn.getPrefix()+":"+qn.getLocalPart()).
append("=\"").
append(escapeXML(att.getValue())).
append("\"");
}
event=getReader().peek();
if(event!=null && event.isEndElement())
{
getReader().nextEvent();
b.append("/>");
}
else
{
b.append(">");
depth++;
}
}
}
return b;
}
protected URI createAnonymousURI() throws URISyntaxException
{
return createURI("_"+(++ID_GENERATOR));
}
public void parse(InputStream in) throws XMLStreamException
{
read(new InputStreamReader(in));
}
public void parse(Reader in) throws XMLStreamException
{
read(in);
}
public void parse(File in) throws XMLStreamException
{
try {
debug("Parsing file "+in);
FileReader fin= new FileReader(in);
read(fin);
fin.close();
debug("End Parsing file "+in);
}
catch (IOException e)
{
throw new XMLStreamException(e);
}
}
public void parse(URL in) throws XMLStreamException
{
debug("parsing URL "+in);
try {
InputStream fin= in.openStream();
read(new InputStreamReader(fin));
fin.close();
}
catch (IOException e)
{
throw new XMLStreamException(e);
}
}
private String name2string(StartElement e)
{
return name2string(e.getName());
}
private String name2string(QName name)
{
return name.getNamespaceURI()+name.getLocalPart();
}
private URI name2uri(QName name) throws URISyntaxException
{
return createURI(name2string(name));
}
private URI createURI(String uriAsString) throws URISyntaxException
{
return new URI(uriAsString);
}
private static String escapeXML(String s)
{
return XMLUtilities.escape(s);
}
/**
* echo URLs
* @param args
*/
public static void main(String[] args)
{
try {
Debug.setDebugging(false);
int optind=0;
while(optind<args.length)
{
if(args[optind].equals("-h"))
{
System.err.println(Compilation.getLabel());
System.err.println("\t-h this screen");
System.err.println("\t-d turns debugging on");
return;
}
else if(args[optind].equals("-d"))
{
Debug.setDebugging(true);
}
else if(args[optind].equals("--"))
{
++optind;
break;
}
else if(args[optind].startsWith("-"))
{
throw new IllegalArgumentException("Unknown option "+args[optind]);
}
else
{
break;
}
++optind;
}
System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
System.out.println("<rdf:RDF xmlns:rdf=\""+RDF.NS+"\">");
RDFHandler h= new RDFHandler()
{
@Override
public void found(RDFEvent event) {
System.out.println(event);
}
};
while(optind< args.length)
{
h.parse(new URL(args[optind++]));
}
System.out.println("</rdf:RDF>");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Show details
Hide details
Change log
r239
by plindenbaum on Mar 7, 2009
Diff
cont
Go to:
...rg/lindenb/sw/io/RDFHandler.java
/trunk/src/xsl/xml2html.xsl
Project members,
sign in
to write a code review
Older revisions
r129
by plindenbaum on Aug 29, 2008
Diff
small changes
r27
by plindenbaum on Nov 2, 2007
Diff
continue
r26
by plindenbaum on Nov 2, 2007
Diff
stuff about semantic web
All revisions of this file
File info
Size: 16798 bytes, 780 lines
View raw file
Powered by
Google Project Hosting