ptstring._java


// String handling and parser


	// change " to "
	String resolveQuotes( String s ){
		if(s==null) return null;
   		int i=0,length = s.length();

    	if( length < 6 ) return s;

    	StringBuffer w = new StringBuffer(0);
    	
   		for(i=0; i<length-5; i++){
   			if( s.substring(i, i+6).equalsIgnoreCase( """ ) ){
   				w.append('\"');
   				i+=5;
   			}else{
   				w.append(s.charAt(i));
   			}
   		}
   		w.append(s.substring(i, length));
   		
   		return w.toString();
   	}


 	// Remove White space characters before and after s
 	String stripWhiteSpace( String s ){ 
 		if(s == null) return null;
 		
 		int i=0, l=s.length(), k=l-1;
 		
 		while(i<l && (s.charAt(i) == ' ' || s.charAt(i) == '\r' || s.charAt(i) == '\n' || s.charAt(i) == '\t')) i++;
 		
 		if(i==l) return null;
 		
 		while(k>=0 && (s.charAt(k) == ' ' || s.charAt(k) == '\r' || s.charAt(k) == '\n' || s.charAt(k) == '\t')) k--;
 		
 		if(k<0 || k<i) return null;
 
 		return s.substring( i, k+1 );
  	}		
				 		

// Get size of Textwindow
	
Dimension string_textWindowSize( Graphics g, String s ){
	FontMetrics fontMetrics = g.getFontMetrics();
		
	int fromIndex = 0,toIndex;
	int lines = 1, width = 0,w;
	while( (toIndex = s.indexOf('|', fromIndex)) != -1 && toIndex < s.length()-1){
		w = fontMetrics.stringWidth(s.substring(fromIndex,toIndex));
		if(w>width) width = w;
		lines++;
		fromIndex = toIndex+1;
	}
		
	w = fontMetrics.stringWidth(s.substring(fromIndex));
	if(w>width) width = w;

	Dimension d = new Dimension( width + 10,
				  lines * fontMetrics.getHeight() + fontMetrics.getHeight()/2);
	return d;
}

void string_drawTextWindow( Graphics g, int x, int y, Dimension d, Color c, String s, int corner ){ 	
	g.clearRect(x, y, d.width, d.height);
	if( c == null )
       g.setColor(Color.black);
    else
       g.setColor(c);	
     
    FontMetrics f = g.getFontMetrics();
    int fromIndex = 0, toIndex;
	int lines = 1;
	while( (toIndex = s.indexOf('|', fromIndex)) != -1 && toIndex < s.length()-1){
		g.drawString( s.substring(fromIndex,toIndex), 
					  x + 5, y + lines*f.getHeight());
		lines++;
		fromIndex = toIndex+1;
	}
	g.drawString( s.substring(fromIndex), x+5, y + lines*f.getHeight());
	
	switch(corner){
		case 1: g.fillRect(x,  y+d.height-2, 2, 2); break;
		case 2: g.fillRect(x, y, 2, 2); 			 break;
		case 3: g.fillRect(x+d.width-2, y+d.height-2, 2, 2); break;
		case 4: g.fillRect(x+d.width-2, y, 2, 2); break;
	}
}


	/** Read parameter values from a list of parameter tags.
	* The list has the syntax <p>
	*<CODE>{param1=value1} {param2=value2} {param3=value3}</CODE>
    * @param p The list string.
    * @param param The parameter name.
	*/
	public String myGetParameter( String p, String param ){
		String r;
		
		if( p == null ){
			r = resolveQuotes( getParameter( param ) );
			if( r != null)
				return r;
		}else{
			r = extractParameter( p, param );
			if( r != null)
				return r;
		}

		return extractParameter( PTViewer_Properties, param );
	}	
	
	String extractParameter( String p, String param ){
		int ia = 0, ie = 0;
		
		if( p==null || param==null ) return null;
		
		while( (ia = p.indexOf('{', ie)) >= 0 && (ie = p.indexOf('}',ia)) >= 0 ){
			String s = stripWhiteSpace( p.substring( ia+1, ie ) );
			if( s.startsWith( param + "=" ) )
				return( resolveQuotes(stripWhiteSpace( s.substring( s.indexOf('=')+1 ))));
		}
		return null;
	}			
	
	int getNextWord(int i, String s, StringBuffer w){
		int k = i;
		int length = s.length();
	
		
		if( i>=length ) return i;

		if( s.charAt(i) == '\'' ){
			i++;
			if( i== length ){ w.setLength(0); return i;}
			k = i;
			while( i<length && s.charAt(i) != '\'' ) i++;
			if( i < length ){
				w.insert(0, s.substring(k,i));
				w.setLength(s.substring(k,i).length());
   			}else{
				w.insert(0, s.substring(k));
				w.setLength(s.substring(k).length());
			}
   			return i;
   		}
		
		if( s.charAt(i) == '$' ){
			i++;
			if( i== length ){ w.setLength(0); return i;}
			char sep = s.charAt(i);
			i++;
			if( i== length ){ w.setLength(0); return i;}			
			k = i;
			while( i<length && s.charAt(i) != sep ) i++;
			if( i < length ){
				w.insert(0, s.substring(k,i));
				w.setLength(s.substring(k,i).length());
   			}else{
				w.insert(0, s.substring(k));
				w.setLength(s.substring(k).length());
			}
   			return i;
   		}
		

		
   		while( i<length && s.charAt(i) != ' ' && s.charAt(i) != '\r' && s.charAt(i) != '\n' && s.charAt(i) != '\t' ) i++;
   		if( i < length ){
			w.insert(0, s.substring(k,i));
			w.setLength(s.substring(k,i).length());
   		}else{
			w.insert(0, s.substring(k));
			w.setLength(s.substring(k).length());
		}

  		return i;
  	}
 
 
final String getArg( int n, String args, char sep ){
		int i, index = 0, end;
		
		if( args == null )
			return null;
			
		for( i=0; i<n; i++ ){
			index = args.indexOf(sep, index);
			if( index == -1 ) return null;
			index++;
		}
		end = args.indexOf(sep, index);
		if( end == -1 )
			return args.substring( index );
		else
			return args.substring( index, end);
	}

final String getArg( int n, String args ){ 
		return getArg( n, args, ',' );
	}
	
final int getNumArgs( String args ){
		return getNumArgs( args, ',' );
	}
	
final int getNumArgs( String args, char sep ){
		int i, index = 0;
		
		if( args == null )
			return 0;
			
		for( i=1; (index = args.indexOf(sep, index)) != -1; i++ )
			index++;
		
		return i;
	}
		

Back