You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by un...@apache.org on 2003/10/27 18:39:10 UTC
cvs commit: cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/transformation SourcePropsWritingTransformer.java
unico 2003/10/27 09:39:10
Modified: src/blocks/repository/java/org/apache/cocoon/transformation
SourcePropsWritingTransformer.java
Log:
change props element to set, add remove functionality
Revision Changes Path
1.2 +70 -24 cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/transformation/SourcePropsWritingTransformer.java
Index: SourcePropsWritingTransformer.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/repository/java/org/apache/cocoon/transformation/SourcePropsWritingTransformer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- SourcePropsWritingTransformer.java 24 Oct 2003 19:33:46 -0000 1.1
+++ SourcePropsWritingTransformer.java 27 Oct 2003 17:39:10 -0000 1.2
@@ -95,13 +95,15 @@
/** incoming elements */
public static final String PATCH_ELEMENT = "patch";
public static final String SOURCE_ELEMENT = "source";
- public static final String PROPS_ELEMENT = "props";
+ public static final String SET_ELEMENT = "set";
+ public static final String REMOVE_ELEMENT = "remove";
/** The current state */
private static final int STATE_OUTSIDE = 0;
private static final int STATE_PATCH = 1;
private static final int STATE_SOURCE = 2;
- private static final int STATE_PROPS = 3;
+ private static final int STATE_SET = 3;
+ private static final int STATE_REMOVE = 4;
private int state;
@@ -148,7 +150,7 @@
*/
public void startTransformingElement(String uri, String name, String raw, Attributes attr)
throws SAXException, IOException, ProcessingException {
- if (this.getLogger().isDebugEnabled() == true) {
+ if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("BEGIN startTransformingElement uri=" + uri +
", name=" + name + ", raw=" + raw + ", attr=" + attr);
}
@@ -163,10 +165,12 @@
this.startTextRecording();
// Element: props
- } else if (this.state == STATE_PATCH && name.equals(PROPS_ELEMENT)) {
- this.state = STATE_PROPS;
+ } else if (this.state == STATE_PATCH && name.equals(SET_ELEMENT)) {
+ this.state = STATE_SET;
+ this.startRecording();
+ } else if (this.state == STATE_PATCH && name.equals(REMOVE_ELEMENT)) {
+ this.state = STATE_REMOVE;
this.startRecording();
-
} else {
super.startTransformingElement(uri, name, raw, attr);
}
@@ -197,41 +201,62 @@
}
// Element: patch
- if ((name.equals(PATCH_ELEMENT) && this.state == STATE_PATCH)) {
+ if ((this.state == STATE_PATCH && name.equals(PATCH_ELEMENT))) {
this.state = STATE_OUTSIDE;
String sourceName = null;
String tag = null;
- DocumentFragment frag = null;
+ DocumentFragment setfrag = null;
+ DocumentFragment removefrag = null;
do {
tag = (String)this.stack.pop();
if (tag.equals(SOURCE_ELEMENT)) {
sourceName = (String)this.stack.pop();
- } else if (tag.equals(PROPS_ELEMENT)) {
- frag = (DocumentFragment)this.stack.pop();
+ } else if (tag.equals(SET_ELEMENT)) {
+ setfrag = (DocumentFragment)this.stack.pop();
+ } else if (tag.equals(REMOVE_ELEMENT)) {
+ removefrag = (DocumentFragment)this.stack.pop();
}
} while ( !tag.equals("END") );
- NodeList list = frag.getChildNodes();
- Node node = null;
- for (int i=0; i<list.getLength(); i++) {
- node = list.item(i);
- if (node instanceof Element) {
- this.patchSource(sourceName, (Element)node);
+ if (setfrag != null) {
+ NodeList list = setfrag.getChildNodes();
+ Node node = null;
+ for (int i=0; i<list.getLength(); i++) {
+ node = list.item(i);
+ if (node instanceof Element) {
+ this.setProperty(sourceName, (Element) node);
+ }
+ }
+ }
+ if (removefrag != null) {
+ NodeList list = removefrag.getChildNodes();
+ Node node = null;
+ for (int i=0; i<list.getLength(); i++) {
+ node = list.item(i);
+ if (node instanceof Element) {
+ this.removeProperty(sourceName, (Element) node);
+ }
}
}
// Element: source
- } else if (name.equals(SOURCE_ELEMENT) && this.state == STATE_SOURCE) {
+ } else if (this.state == STATE_SOURCE && name.equals(SOURCE_ELEMENT)) {
this.state = STATE_PATCH;
String sourceName = this.endTextRecording();
this.stack.push(sourceName);
this.stack.push(SOURCE_ELEMENT);
- // Element: props
- } else if (name.equals(PROPS_ELEMENT) == true && this.state == STATE_PROPS) {
+ // Element: set
+ } else if (this.state == STATE_SET && name.equals(SET_ELEMENT)) {
this.state = STATE_PATCH;
this.stack.push(this.endRecording());
- this.stack.push(PROPS_ELEMENT);
+ this.stack.push(SET_ELEMENT);
+ // Element: remove
+ } else if (this.state == STATE_REMOVE && name.equals(REMOVE_ELEMENT)) {
+ this.state = STATE_PATCH;
+ this.stack.push(this.endRecording());
+ this.stack.push(REMOVE_ELEMENT);
+
// default
} else {
super.endTransformingElement(uri, name, raw);
@@ -242,13 +267,13 @@
}
}
- private void patchSource(String src, Element value)
+ private void setProperty(String src, Element element)
throws ProcessingException, IOException, SAXException {
- if (src != null && value != null) {
+ if (src != null && element != null) {
try {
Source source = this.resolver.resolveURI(src);
if (source instanceof InspectableSource) {
- SourceProperty property = new SourceProperty(value);
+ SourceProperty property = new SourceProperty(element);
((InspectableSource)source).setSourceProperty(property);
} else {
@@ -262,5 +287,26 @@
this.getLogger().error("Error setting properties on "+src);
}
}
+
+ private void removeProperty(String src, Element element)
+ throws ProcessingException, IOException, SAXException {
+
+ if (src != null && element != null) {
+ try {
+ Source source = this.resolver.resolveURI(src);
+ if (source instanceof InspectableSource) {
+ ((InspectableSource)source).removeSourceProperty(
+ element.getNamespaceURI(),element.getLocalName());
+ } else {
+ this.getLogger().error("Cannot remove properties on " + src +
+ ": not an inspectable source");
+ }
+ } catch (Exception e) {
+ throw new ProcessingException("Error removing properties on "+src, e);
+ }
+ } else {
+ this.getLogger().error("Error removing properties on "+src);
+ }
+ }
}