Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DeclareStep.java
*
* Copyright 2008 Mark Logic Corporation.
* Portions Copyright 2007 Sun Microsystems, Inc.
* All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* https://xproc.dev.java.net/public/CDDL+GPL.html or
* docs/CDDL+GPL.txt in the distribution. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at docs/CDDL+GPL.txt.
*/
package com.xmlcalabash.model;
import com.xmlcalabash.core.XProcData;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.QName;
import com.xmlcalabash.core.XProcRuntime;
import java.util.Collection;
import java.util.Vector;
import java.util.Hashtable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcException;
public class DeclareStep extends CompoundStep {
protected boolean psviRequired = false;
protected String xpathVersion = "2.0";
private QName declaredType = null;
private boolean atomic = true;
private Pipeline implementation = null;
protected Hashtable declaredSteps = new Hashtable ();
protected HashSet importedLibs = new HashSet ();
private DeclareStep parentDecl = null;
private Vector rest = null;
private HashSet excludedInlineNamespaces = null;
// If a pipeline contains both import statements and inlined step declarations
// then we have to be careful not to parse declared steps twice (we will have
// parsed the imported ones, but not the inlined ones. This flag keeps track
// of whether we've parsed the body of a step declaration or not.
// FIXME: Maybe this should be managed by the parser not the DeclareStep?
private boolean bodyParsed = false;
/** Creates a new instance of DeclareStep */
public DeclareStep(XProcRuntime xproc, XdmNode node, String name) {
super(xproc, node, XProcConstants.p_declare_step, name);
}
protected void setXmlContent(Vector nodes) {
rest = nodes;
}
protected Vector getXmlContent() {
return rest;
}
public boolean getBodyParsed() {
return bodyParsed;
}
public void setBodyParsed(boolean parsed) {
bodyParsed = parsed;
}
public void setPsviRequired(boolean psvi) {
psviRequired = psvi;
}
public void setXPathVersion(String version) {
xpathVersion = version;
}
public void setDeclaredType(QName type) {
declaredType = type;
}
public void setExcludeInlineNamespaces(HashSet uris) {
excludedInlineNamespaces = uris;
}
public HashSet getExcludeInlineNamespaces() {
return excludedInlineNamespaces;
}
public void setAtomic(boolean isAtomic) {
atomic = isAtomic;
}
public boolean isAtomic() {
return atomic;
}
public boolean isPipeline() {
return !atomic;
}
public QName getDeclaredType() {
return declaredType;
}
public void setParentDecl(DeclareStep decl) {
parentDecl = decl;
}
public void setPipeline(Pipeline pipeline) {
implementation = pipeline;
}
public Pipeline getPipeline() {
return implementation;
}
public void declareStep(QName type, DeclareStep step) {
if (declaredSteps.containsKey(type)) {
throw new XProcException(step, "Duplicate step type: " + type);
} else {
declaredSteps.put(type, step);
}
}
public boolean imported(String uri) {
if (importedLibs.contains(uri)) {
return true;
}
if (parentDecl == null) {
return false;
}
return parentDecl.imported(uri);
}
public void addImport(String uri) {
importedLibs.add(uri);
}
public DeclareStep getDeclaration() {
return getStepDeclaration(declaredType);
}
public DeclareStep getStepDeclaration(QName type) {
if (declaredSteps.containsKey(type)) {
return declaredSteps.get(type);
} else if (parentDecl != null) {
return parentDecl.getStepDeclaration(type);
} else {
return runtime.getBuiltinDeclaration(type);
}
}
public Collection getStepDeclarations() {
return declaredSteps.values();
}
public void setupEnvironment() {
setEnvironment(new Environment(this));
}
protected void patchEnvironment(Environment env) {
if (atomic) {
//nop;
} else {
// See if there's exactly one "ordinary" input
int count = 0;
Input defin = null;
boolean foundPrimary = false;
for (Input input : inputs) {
if (!input.getPort().startsWith("|") && !input.getParameterInput()) {
count++;
foundPrimary |= input.getPrimary();
if (!input.getPrimary() && input.getPrimarySet()) {
// nop; if the port is explicitly marked primary=false, it can't count
} else {
if (defin == null || input.getPrimary()) {
defin = input;
}
}
}
}
if (count == 1 || foundPrimary) {
env.setDefaultReadablePort(defin);
}
}
}
private int logLevel(Logger logger) {
Logger log = logger;
Level level = null;
if (log != null) {
level = log.getLevel();
}
while (log != null && level == null) {
log = log.getParent();
level = log.getLevel();
}
if (level == null) {
// WTF!?
return Level.SEVERE.intValue();
} else {
return level.intValue();
}
}
public void setup() {
XProcRuntime runtime = this.runtime;
DeclareStep decl = this;
boolean debug = runtime.getDebug();
if (decl.psviRequired && !runtime.getPSVISupported()) {
throw XProcException.dynamicError(22);
}
if (debug && logLevel(logger) <= Level.FINEST.intValue()) {
System.err.println("=====================================================================================");
System.err.println("Before augment:");
decl.dump();
}
boolean seenPrimaryDocument = false;
boolean seenPrimaryParameter = false;
for (Input input : decl.inputs()) {
if (!input.getPort().startsWith("|") && input.getPrimary()) {
if (seenPrimaryDocument && !input.getParameterInput()) {
error("At most one primary document input port is allowed", XProcConstants.staticError(30));
}
if (seenPrimaryParameter && input.getParameterInput()) {
error("At most one primary parameter input port is allowed", XProcConstants.staticError(30));
}
if (input.getParameterInput()) {
seenPrimaryParameter = true;
} else {
seenPrimaryDocument = true;
}
}
}
boolean seenPrimary = false;
for (Output output : decl.outputs()) {
if (!output.getPort().endsWith("|") && output.getPrimary()) {
if (seenPrimary) {
error("At most one primary output port is allowed", XProcConstants.staticError(30));
}
seenPrimary = true;
}
}
if (debug && logLevel(logger) <= Level.FINEST.intValue()) {
System.err.println("After binding pipeline inputs and outputs:");
decl.dump();
}
if (subpipeline.size() == 0) {
error("Declared step has no subpipeline, but is not known.", XProcConstants.staticError(100)); // FIXME!
return;
}
decl.augment();
if (debug && logLevel(logger) <= Level.FINEST.intValue()) {
System.err.println("After augment:");
decl.dump();
}
decl.setupEnvironment();
if (!decl.valid()) {
if (logLevel(logger) <= Level.INFO.intValue()) {
decl.dump();
}
return;
}
if (debug && logLevel(logger) <= Level.FINEST.intValue()) {
System.err.println("After valid:");
decl.dump();
}
if (!decl.orderSteps()) {
if (logLevel(logger) <= Level.INFO.intValue()) {
decl.dump();
}
return;
}
if (debug && logLevel(logger) <= Level.FINEST.intValue()) {
System.err.println("After ordering:");
decl.dump();
}
HashSet vars = new HashSet ();
checkDuplicateVars(vars);
// Are all the primary outputs bound?
if (!checkOutputBindings()) {
if (logLevel(logger) <= Level.INFO.intValue()) {
decl.dump();
}
return;
}
}
protected boolean checkOutputBindings() {
HashSet