view software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdltext/InsertAtCharPos.java @ 6:2396a569e446

new functions: externalObjects, normalizer, Unicode2Betacode
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 08 Feb 2011 14:54:09 +0100
parents
children
line wrap: on
line source

/*
 *  eXist Open Source Native XML Database: Extension module
 *  Copyright (C) 2008 Josef Willenborg
 *  jwillenborg@mpiwg-berlin.mpg.de
 *  http://www.mpiwg-berlin.mpg.de
 *  
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id: TextModule.java $
 */
package org.exist.xquery.modules.mpdltext;

import org.exist.dom.QName;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.StringValue;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.ValueSequence;

public class InsertAtCharPos extends BasicFunction {

	public final static FunctionSignature signature =
		new FunctionSignature(
			new QName("insertAtCharPos", MPDLTextModule.NAMESPACE_URI, MPDLTextModule.PREFIX),
			"A function which inserts in the xml element node string (first parameter) the given xml element " +
			"node string (second parameter) at the given char position (third parameter).",
			new SequenceType[] { new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
			                     new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),  
			                     new SequenceType(Type.INT, Cardinality.EXACTLY_ONE)
			                    },
			new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));

	public InsertAtCharPos(XQueryContext context) {
		super(context, signature);
	}

	public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
	  Sequence elementInputStr = args[0];
    Sequence insertElementStr = args[1];
    Sequence charPosStr = args[2];
    String elementInputStrStr = elementInputStr.getStringValue();
    String insertElementStrStr = insertElementStr.getStringValue();
    String charPosStrStr = charPosStr.getStringValue();
    Integer charPos = new Integer(charPosStrStr);
    int strCharIndex = getIndex(elementInputStrStr, charPos);
    if (charPos == 0)
      strCharIndex = getIndex(elementInputStrStr, charPos + 1) - 1;
    String resultStr = elementInputStrStr.substring(0, strCharIndex) + insertElementStrStr + elementInputStrStr.substring(strCharIndex);
		ValueSequence result = new ValueSequence();
    result.add(new StringValue(resultStr));
		return result;
	}

	private int getIndex(String xmlString, int charPos) {
	  int size = xmlString.length();
	  int counter = 0;
    int charCounter = 0;
    int counterLastChar = -1;
	  boolean isEntity = false;
    boolean isElement = false;
	  while (counter < size) {
      char c = xmlString.charAt(counter);
      switch (c) {
        case '<': isElement = true; break; 
        case '>': isElement = false; break;
        case '&': isEntity = true; break;
        case ';': isEntity = false; break;
      }
      // count all chars which are not inside elements and entities
      // if element closing char ">" is found it should not be counted as a char
      // if an entity closing char ";" is found it should be counted cause the entity itself is one char long
      if (! isEntity && ! isElement && !(c == '>')) { 
        charCounter++;
        counterLastChar = counter;
      }
      if (charCounter == charPos) {
        break;
      }
      counter++;
	  }
	  // input charPos was bigger than available chars: return the last available charPos 
	  if (counter == size)
	    return counterLastChar + 1;
	  return counter + 1;
	}
}