| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans.packages; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.PipelineConfiguration; |
| 12 |
import net.sf.saxon.event.ProxyReceiver; |
| 13 |
import net.sf.saxon.event.Sender; |
| 14 |
import net.sf.saxon.event.Sink; |
| 15 |
import net.sf.saxon.expr.parser.Location; |
| 16 |
import net.sf.saxon.lib.ParseOptions; |
| 17 |
import net.sf.saxon.om.NodeName; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.SchemaType; |
| 20 |
import net.sf.saxon.type.SimpleType; |
| 21 |
|
| 22 |
import javax.xml.transform.stream.StreamSource; |
| 23 |
import java.io.File; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class PackageInspector extends ProxyReceiver { |
| 34 |
|
| 35 |
private String packageName; |
| 36 |
private String packageVersion = "1"; |
| 37 |
private int elementCount = 0; |
| 38 |
|
| 39 |
private PackageInspector(PipelineConfiguration pipe) { |
| 40 |
super(new Sink(pipe)); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public void startElement(NodeName namecode, SchemaType typecode, Location location, int properties) |
| 48 |
throws XPathException { |
| 49 |
if (elementCount++ >= 1) { |
| 50 |
|
| 51 |
throw new XPathException("#start#"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@Override |
| 71 |
public void attribute(NodeName name, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException { |
| 72 |
if (name.getLocalPart().equals("name")) { |
| 73 |
packageName = value.toString(); |
| 74 |
} else if (name.getLocalPart().equals("package-version") || name.getLocalPart().equals("packageVersion")) { |
| 75 |
packageVersion = value.toString(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
private VersionedPackageName getNameAndVersion() { |
| 80 |
if (packageName == null) { |
| 81 |
return null; |
| 82 |
} |
| 83 |
try { |
| 84 |
return new VersionedPackageName(packageName, packageVersion); |
| 85 |
} catch (XPathException e) { |
| 86 |
return null; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public static PackageDetails getPackageDetails(File top, Configuration config) { |
| 91 |
PackageInspector inspector = new PackageInspector(config.makePipelineConfiguration()); |
| 92 |
try { |
| 93 |
Sender.send(new StreamSource(top), inspector, new ParseOptions()); |
| 94 |
} catch (XPathException e) { |
| 95 |
|
| 96 |
} |
| 97 |
VersionedPackageName vp = inspector.getNameAndVersion(); |
| 98 |
if (vp == null) { |
| 99 |
return null; |
| 100 |
} else { |
| 101 |
PackageDetails details = new PackageDetails(); |
| 102 |
details.nameAndVersion = vp; |
| 103 |
details.sourceLocation = new StreamSource(top); |
| 104 |
return details; |
| 105 |
} |
| 106 |
} |
| 107 |
} |