1
1
package com .nwalsh .sinclude ;
2
2
3
3
import com .nwalsh .DebuggingLogger ;
4
- import com .nwalsh .sinclude .exceptions .*;
5
- import com .nwalsh .sinclude .schemes .*;
4
+ import com .nwalsh .sinclude .exceptions .XIncludeException ;
5
+ import com .nwalsh .sinclude .exceptions .XIncludeFallbackException ;
6
+ import com .nwalsh .sinclude .exceptions .XIncludeLoopException ;
7
+ import com .nwalsh .sinclude .exceptions .XIncludeNoFragmentException ;
8
+ import com .nwalsh .sinclude .exceptions .XIncludeSyntaxException ;
9
+ import com .nwalsh .sinclude .schemes .ElementScheme ;
10
+ import com .nwalsh .sinclude .schemes .RFC5147Scheme ;
11
+ import com .nwalsh .sinclude .schemes .SearchScheme ;
12
+ import com .nwalsh .sinclude .schemes .XPathScheme ;
13
+ import com .nwalsh .sinclude .schemes .XmlnsScheme ;
14
+ import com .nwalsh .sinclude .utils .NodeUtils ;
6
15
import com .nwalsh .sinclude .utils .ReceiverUtils ;
7
- import com .nwalsh .sinclude .xpointer .*;
16
+ import com .nwalsh .sinclude .xpointer .DefaultFragmentIdParser ;
17
+ import com .nwalsh .sinclude .xpointer .FragmentIdParser ;
18
+ import com .nwalsh .sinclude .xpointer .ParseType ;
19
+ import com .nwalsh .sinclude .xpointer .Scheme ;
20
+ import com .nwalsh .sinclude .xpointer .SchemeData ;
21
+ import com .nwalsh .sinclude .xpointer .SelectionResult ;
8
22
import net .sf .saxon .event .Receiver ;
9
23
import net .sf .saxon .event .ReceiverOption ;
10
24
import net .sf .saxon .expr .parser .Loc ;
11
- import net .sf .saxon .om .*;
12
- import net .sf .saxon .s9api .*;
25
+ import net .sf .saxon .om .AttributeInfo ;
26
+ import net .sf .saxon .om .AttributeMap ;
27
+ import net .sf .saxon .om .EmptyAttributeMap ;
28
+ import net .sf .saxon .om .FingerprintedQName ;
29
+ import net .sf .saxon .om .NodeInfo ;
30
+ import net .sf .saxon .om .NodeName ;
31
+ import net .sf .saxon .s9api .Axis ;
32
+ import net .sf .saxon .s9api .DocumentBuilder ;
33
+ import net .sf .saxon .s9api .Processor ;
34
+ import net .sf .saxon .s9api .QName ;
35
+ import net .sf .saxon .s9api .SaxonApiException ;
36
+ import net .sf .saxon .s9api .Serializer ;
37
+ import net .sf .saxon .s9api .XdmDestination ;
38
+ import net .sf .saxon .s9api .XdmNode ;
39
+ import net .sf .saxon .s9api .XdmNodeKind ;
40
+ import net .sf .saxon .s9api .XdmSequenceIterator ;
13
41
import net .sf .saxon .trans .XPathException ;
14
42
import net .sf .saxon .type .BuiltInAtomicType ;
15
43
16
- import javax .xml .XMLConstants ;
17
44
import java .io .File ;
18
45
import java .net .URI ;
19
- import java .util .*;
46
+ import java .util .Collections ;
47
+ import java .util .HashMap ;
48
+ import java .util .HashSet ;
49
+ import java .util .Stack ;
50
+ import java .util .Vector ;
20
51
import java .util .regex .Matcher ;
21
52
import java .util .regex .Pattern ;
22
53
54
+ import static com .nwalsh .sinclude .utils .NodeUtils .xml_base ;
55
+ import static com .nwalsh .sinclude .utils .NodeUtils .xml_id ;
56
+ import static com .nwalsh .sinclude .utils .NodeUtils .xml_lang ;
57
+
23
58
public class XInclude {
24
59
private static final String NS_XML = "http://www.w3.org/XML/1998/namespace" ;
25
60
private static final String NS_XINCLUDE = "http://www.w3.org/2001/XInclude" ;
@@ -28,10 +63,6 @@ public class XInclude {
28
63
29
64
private static final String localAttrNS = "http://www.w3.org/2001/XInclude/local-attributes" ;
30
65
31
- private static final QName xml_base = new QName ("xml" , XMLConstants .XML_NS_URI , "base" );
32
- private static final QName xml_lang = new QName ("xml" , XMLConstants .XML_NS_URI , "lang" );
33
- private static final QName xml_id = new QName ("xml" , XMLConstants .XML_NS_URI , "id" );
34
-
35
66
private static final QName _set_xml_id = new QName ("" , "set-xml-id" );
36
67
private static final QName _accept = new QName ("" , "accept" );
37
68
private static final QName _accept_language = new QName ("" , "accept-language" );
@@ -355,6 +386,10 @@ public XdmNode process(XdmNode node) throws XPathException {
355
386
if (xptr != null ) {
356
387
Exception lastException = null ;
357
388
boolean success = false ;
389
+
390
+ fragmentIdParser .setProperty (xml_base , node .getParent ().getBaseURI ().toString ());
391
+ fragmentIdParser .setProperty (xml_lang , NodeUtils .getLang (node .getParent ()));
392
+
358
393
Scheme [] pointers = fragmentIdParser .parseFragmentIdentifier (parse , xptr );
359
394
for (Scheme pointer : pointers ) {
360
395
if (!success ) {
@@ -384,6 +419,10 @@ public XdmNode process(XdmNode node) throws XPathException {
384
419
}
385
420
}
386
421
}
422
+
423
+ fragmentIdParser .setProperty (xml_base , null );
424
+ fragmentIdParser .setProperty (xml_lang , null );
425
+
387
426
if (!success ) {
388
427
if (lastException != null ) {
389
428
throw new XIncludeNoFragmentException ("Failed to locate fragment: " + xptr + " (" + lastException .getMessage () + ")" , lastException );
@@ -419,14 +458,17 @@ public XdmNode process(XdmNode node) throws XPathException {
419
458
}
420
459
421
460
private XdmNode fixup (XdmNode xinclude , XdmNode document , String setId ) {
422
- // Fixing up xml:base is usually handled by the fragid processor.
461
+ // Fixup is usually handled by the fragid processor.
462
+
423
463
// It's only ever true here if we're XIncluding a whole document.
424
- // Consequently, fixupLang never applies here.
425
464
if (document .getNodeKind () != XdmNodeKind .DOCUMENT ) {
426
465
// This is an internal error and should never happen
427
466
throw new IllegalArgumentException ("XInclude fixup can only be called on a document" );
428
467
}
429
468
469
+ String contextLanguage = NodeUtils .getLang (xinclude .getParent ());
470
+ String contextBaseURI = NodeUtils .getLang (xinclude .getParent ());
471
+
430
472
try {
431
473
XdmDestination destination = ReceiverUtils .makeDestination (document );
432
474
Receiver receiver = ReceiverUtils .makeReceiver (document , destination );
@@ -473,15 +515,28 @@ private XdmNode fixup(XdmNode xinclude, XdmNode document, String setId) {
473
515
}
474
516
}
475
517
476
- if (getFixupXmlBase ()) {
477
- // If fixupXmlBase is true, this nodes base URI will be correct because either:
478
- // 1. The XPathScheme will have already done fixup or
479
- // 2. The whole document is being XIncluded (in which case fixup is still necessary)
480
- AttributeInfo base = new AttributeInfo (fq_xml_base ,
481
- BuiltInAtomicType .UNTYPED_ATOMIC ,
482
- node .getBaseURI ().toASCIIString (),
483
- Loc .NONE , ReceiverOption .NONE );
484
- amap = amap .put (base );
518
+ if (getFixupXmlBase () && node .getBaseURI () != null ) {
519
+ if (contextBaseURI == null || !contextBaseURI .equals (node .getBaseURI ().toString ())) {
520
+ AttributeInfo base = new AttributeInfo (fq_xml_base ,
521
+ BuiltInAtomicType .UNTYPED_ATOMIC ,
522
+ node .getBaseURI ().toString (),
523
+ Loc .NONE , ReceiverOption .NONE );
524
+ amap = amap .put (base );
525
+ }
526
+ }
527
+
528
+ if (getFixupXmlLang ()) {
529
+ String lang = NodeUtils .getLang (node );
530
+ if (lang == null && contextLanguage != null ) {
531
+ lang = "" ;
532
+ }
533
+ if (lang != null ) {
534
+ AttributeInfo xml_lang = new AttributeInfo (fq_xml_lang ,
535
+ BuiltInAtomicType .UNTYPED_ATOMIC ,
536
+ lang ,
537
+ Loc .NONE , ReceiverOption .NONE );
538
+ amap = amap .put (xml_lang );
539
+ }
485
540
}
486
541
487
542
for (AttributeInfo ainfo : attributes ) {
0 commit comments