01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.scripting.convertors;
21:
22: import java.io.ByteArrayInputStream;
23:
24: import javax.xml.stream.XMLStreamException;
25:
26: import org.apache.axiom.om.OMElement;
27: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
28: import org.apache.bsf.BSFEngine;
29: import org.apache.bsf.BSFException;
30:
31: /**
32: * OMElementConvertor for Ruby scripts
33: *
34: * TODO: Right now this goes via Strings and likely isn't very fast
35: * There could well be much better ways to do this :)
36: */
37: public class RBOMElementConvertor implements OMElementConvertor {
38:
39: protected BSFEngine bsfEngine;
40:
41: public RBOMElementConvertor() {
42: }
43:
44: public Object toScript(OMElement omElement) {
45: try {
46:
47: StringBuffer srcFragment = new StringBuffer(
48: "Document.new(<<EOF\n");
49: srcFragment.append(omElement.toString());
50: srcFragment.append("\nEOF\n");
51: srcFragment.append(")");
52:
53: Object o = bsfEngine.eval("RBOMElementConvertor", 0, 0,
54: srcFragment.toString());
55: return o;
56:
57: } catch (BSFException e) {
58: throw new RuntimeException(e);
59: }
60: }
61:
62: public OMElement fromScript(Object o) {
63: try {
64:
65: byte[] xmlBytes = o.toString().getBytes();
66: StAXOMBuilder builder = new StAXOMBuilder(
67: new ByteArrayInputStream(xmlBytes));
68: OMElement omElement = builder.getDocumentElement();
69:
70: return omElement;
71:
72: } catch (XMLStreamException e) {
73: throw new RuntimeException(e);
74: }
75: }
76:
77: public void setEngine(BSFEngine e) {
78: this.bsfEngine = e;
79: }
80: }
|