001 package com.saelist.stx.operators;
002
003 import java.util.*;
004 import com.saelist.stx.*;
005
006
007 /** See {@link com.saelist.stx.Operator} for general description of the Operator
008 * concept in stx. <p>
009 *
010 * If the pair's text contains <code>pattern</code> then the text to the
011 * right of the pattern is moved to a new child and only the text to the
012 * left of the pattern remains with the original pair. If the pair
013 * already had children the new child is inserted in front of them.
014 *
015 * For example, split on "//*" with pattern = ".*:.*" will transform the
016 * structure
017 * <pre>
018 *
019 * person
020 * name : John
021 * tel : 123
022 *
023 *
024 *</pre>
025 * into
026 * <pre>
027 *
028 * person
029 * name
030 * John
031 * tel
032 * 123
033 *
034 *</pre>
035 */
036 public class SplitOperator extends AbstractOperator {
037
038 private String pattern;
039
040 /**
041 */
042 public void init() {
043 pattern = config.eval1("pattern");
044 }
045
046 /** Performs the split operation on the given pair.
047 */
048 public void operate(Pair pair) {
049 String[] strings = pair.getText().split(pattern, 2);
050 if(strings.length != 2)
051 return;
052 pair.setText(strings[0]);
053 pair.add(0, new Pair(pair, strings[1]));
054 }
055
056
057 }