Debian Codegraph @

net/sf/saxon/trans/packages/PackageInspector.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * The PIGrabber class is a Receiver that looks at an incoming stylesheet document
27 * and extracts the package name and version from the root element; parsing is then
28 * abandoned.
29 *
30 * @author Michael H. Kay
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 * Abort the parse when the first start element tag is found
45 */
46
47 public void startElement(NodeName namecode, SchemaType typecode, Location location, int properties)
48 throws XPathException {
49 if (elementCount++ >= 1) {
50 // abort the parse when the second start element tag is found
51 throw new XPathException("#start#");
52 }
53 }
54
55 /**
56 * Notify an attribute. Attributes are notified after the startElement event, and before any
57 * children. Namespaces and attributes may be intermingled.
58 *
59 * @param name The name of the attribute
60 * @param typeCode The type of the attribute
61 * @param value
62 * @param locationId the location of the node in the source, or of the instruction that created it
63 * @param properties Bit significant value. The following bits are defined:
64 * <dl>
65 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
66 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
67 * </dl>
68 * @throws IllegalStateException: attempt to output an attribute when there is no open element
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 // early exit is expected
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 }