You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by ce...@apache.org on 2004/04/10 15:47:58 UTC
cvs commit: logging-log4j/examples/src/joran/implicit PrintMe.java PrintMeImplicitAction.java NOPAction.java implicit1.xml
ceki 2004/04/10 06:47:58
Added: examples/src/joran/implicit PrintMe.java
PrintMeImplicitAction.java NOPAction.java
implicit1.xml
Log:
Example of a an implicit action and its usage. (A rather contrived axample)
Revision Changes Path
1.1 logging-log4j/examples/src/joran/implicit/PrintMe.java
Index: PrintMe.java
===================================================================
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package joran.implicit;
import joran.calculator.ComputationAction2;
import org.apache.joran.Interpreter;
import org.apache.joran.Pattern;
import org.apache.joran.RuleStore;
import org.apache.joran.action.NewRuleAction;
import org.apache.joran.helper.SimpleRuleStore;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* This example illustrates the usage of NewRuleAction which allows the Joran
* interpreter to learn new rules on the fly from the XML file being
* interpreted.
*
* This example relies heavily on the code from the joran.calculator package.
*
* @author Ceki Güulcü
*/
public class PrintMe {
public static void main(String[] args) throws Exception {
// Uncomment the following line in order to enable log statements generated
// from joran itself.
// org.apache.log4j.BasicConfigurator.configure();
RuleStore ruleStore = new SimpleRuleStore();
// we start with the rule for the top-most (root) element
ruleStore.addRule(new Pattern("*/foo"), new NOPAction());
// Create a new Joran Interpreter and hand it our simple rule store.
Interpreter ji = new Interpreter(ruleStore);
//
// Add an implicit action.
//
ji.addImplcitAction(new PrintMeImplicitAction());
// Create a SAX parser
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser saxParser = spf.newSAXParser();
// Parse the file given as the application's first argument and
// set the SAX ContentHandler to the Joran Interpreter we just created.
saxParser.parse(args[0], ji);
// The file has been parsed and interpreted. We now errors if any.
List errorList = ji.getExecutionContext().getErrorList();
if (errorList.size() > 0) {
System.out.println("The following errors occured:");
for (int i = 0; i < errorList.size(); i++) {
System.out.println("\t" + errorList.get(i));
}
}
}
}
1.1 logging-log4j/examples/src/joran/implicit/PrintMeImplicitAction.java
Index: PrintMeImplicitAction.java
===================================================================
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package joran.implicit;
import org.apache.joran.ExecutionContext;
import org.apache.joran.Pattern;
import org.apache.joran.action.ImplicitAction;
import org.xml.sax.Attributes;
/**
*
* A rather trivial implicit action which is applicable as soon as an
* element has a printme attribute set to true.
*
* @author Ceki Gülcü
*/
public class PrintMeImplicitAction extends ImplicitAction {
public boolean isApplicable(
Pattern pattern, Attributes attributes, ExecutionContext ec) {
String printmeStr = attributes.getValue("printme");
return Boolean.valueOf(printmeStr).booleanValue();
}
public void begin(ExecutionContext ec, String name, Attributes attributes) {
System.out.println("Element <"+name+"> asked to be printed.");
}
public void end(ExecutionContext ec, String name) {
}
}
1.1 logging-log4j/examples/src/joran/implicit/NOPAction.java
Index: NOPAction.java
===================================================================
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package joran.implicit;
import org.apache.joran.ExecutionContext;
import org.apache.joran.action.Action;
import org.xml.sax.Attributes;
/**
* No operation (NOP) action that does strictly nothing.
*
* @author Ceki Gülcü
*/
public class NOPAction extends Action {
public void begin(ExecutionContext ec, String name, Attributes attributes) {
}
public void end(ExecutionContext ec, String name) {
}
}
1.1 logging-log4j/examples/src/joran/implicit/implicit1.xml
Index: implicit1.xml
===================================================================
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE foo>
<foo>
<!-- These elements will print due to the implcit rule -->
<xyz printme="true">
<abc printme="true"/>
</xyz>
<!-- This element has no associated rule and no implicit rule
applies for it because the printme attribute is not set. -->
<xyz/>
<!-- This element will not be printed even if its printme
attribute is set because implicit rules are invoked only
if no explicit rule matches the element. The */foo rule
mathches the following element.
-->
<foo printme="true"/>
</foo>
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org