The Artima Developer Community
Sponsored Link

Java Answers Forum
Help with replacing parts of a file

5 replies on 1 page. Most recent reply: Apr 19, 2002 12:37 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Help with replacing parts of a file Posted: Apr 18, 2002 11:21 AM
Reply to this message Reply
Advertisement
I have an app that reads in the contents of file into a StringBuffer. It then reads the contents of another file which contains a list of certain words and phrases to replace in the original file. The list is in the following format:
Word to replace;Replacing word
I created this app to be able to speed up the replacement of xml tags in an xml document. (At my work, we're changing all the tags of our existing xml documents). So for example, the replacement list might look like this:
<SingleSelect>;<SelectSingle>
My app would go through the contents of the xml file, and every time it finds the phrase <SingleSelect> (including the angle brackets), it would replace it with <SelectSingle>. The problem is that some tags that need to be replaced are being named differently based on their position. For example:
The original document:

<Name>
<SingleSelect>
<Name>Test</Name>
</SingleSelect>
</Name>

The changed document:

<PageName>
<SelectSingle>
<Name>Test</Name>
</SelectSingle>
</PageName>

I'm not sure how I should distinguish the difference in the replacement list, and how I would get my app to do that? I've posted the code below:
import java.io.*;
import javax.swing.*;
 
public class Test
{
	private String fileName;
	
	private BufferedReader replaceList;
	
	private StringBuffer fileContents = new StringBuffer();
		
	public Test() throws FileNotFoundException, IOException
	{
		String fileName = "";
		String replaceList = "";
		JFileChooser fc = new JFileChooser();
		fc.setApproveButtonText("Open file for replacement");
		int returnVal = fc.showDialog(null, null);
		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			File temp = fc.getSelectedFile();
			fileName = temp.toString();
		}
 
		fc.setApproveButtonText("Open list of replacement words");
		returnVal = fc.showDialog(null, null);
		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			File temp = fc.getSelectedFile();
			replaceList = temp.toString();
		}
		
		if (!fileName.equals("") || !replaceList.equals(""))
		{
			System.exit(0);
		}
	
		start(fileName, replaceList);
	}
	
	public void start(String fileName, String replaceFile) throws FileNotFoundException, IOException
	{
		this.fileName = fileName;
		
		getFileContents();
		
		replaceList = new BufferedReader(new FileReader(replaceFile));
		
		replaceContent();
		
		finish();
	}
	
	private void getFileContents() throws FileNotFoundException, IOException
	{
		InputStream in = new FileInputStream(fileName);
		
		int b = 0;
		final int LINEFEED = 10;
		
		b = in.read();
		while(b != -1)
		{
			if (b == LINEFEED)
			{
				fileContents.append("\n");
			} else
			{
				fileContents.append((char)b);
			}
			b = in.read();
		}
	}
	
	private void replaceContent() throws IOException
	{
		StringBuffer replacedWord = new StringBuffer();
		StringBuffer replacingWord = new StringBuffer();
 
		StringBuffer line = new StringBuffer();
		
		int semicolonIndex = -1;
		
		line.append(replaceList.readLine());
		while(line != null)
		{
			semicolonIndex = line.toString().indexOf(";");
			
			replacedWord.append(line.substring(0, semicolonIndex - 1));
			replacingWord.append(line.substring(semicolonIndex, line.length()));
	
			replaceAllWords(replacedWord, replacingWord);
			
			line.replace(0, line.length(), "");
			replacedWord.replace(0, replacedWord.length(), "");
			replacingWord.replace(0, replacingWord.length(), "");			
		}
	}
	
	private void replaceAllWords(StringBuffer replacedWord, StringBuffer replacingWord)
	{
		int index = -1;
		index = fileContents.toString().indexOf(replacedWord.toString());
		while (index != -1)
		{
			fileContents.replace(index, replacedWord.length(), replacingWord.toString());
		}
	}
	
	private void finish() throws FileNotFoundException, IOException
	{
		OutputStream out = new FileOutputStream(fileName);
		
		int b = 0;
		
		for (int i=0; i<fileContents.length(); i++)
		{
			b = (int) fileContents.charAt(i);
			
			out.write(b);
		}
	}
	
	public static void main(String[] args)
	{
		Test app;
		try
		{
			app = new Test();
		} catch(FileNotFoundException e)
		{
		} catch (IOException e)
		{
		}
	}
}

Thanks for the help.
Hiran


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Help with replacing parts of a file Posted: Apr 18, 2002 4:06 PM
Reply to this message Reply
Hiran,
I haven't been tru' your code...
but may be this URL could be helpfull :
http://www.javaregex.com

Thomas,

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Help with replacing parts of a file Posted: Apr 18, 2002 4:19 PM
Reply to this message Reply
I like IBM's free docs

::: Regex for Java
A powerful, high-performance, regular expression library for Java. With
Regex for Java, a search can be done for a string matching a regular
expression pattern in your application. Regex for Java has been updated
with a bug fix for the problem with Java 2 SDK 1.4.
http://www.alphaWorks.ibm.com/tech/regex4j?open&l=LT030402,t=awfl

I hope you enjoy it as much as I do !

Thomas,

Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Help with replacing parts of a file Posted: Apr 18, 2002 7:00 PM
Reply to this message Reply
I haven't quite tested my app yet, but I'm assuming that it works. Assuming that it does, I should be able to take a file such as:

<Name>

<TagOne>
<ITO>
<Name>FirstElement</Name>
<Test>Hello World</Test>
</ITO>
</TagOne>

</Name>

and a replacement list such as:

<TagOne>;<FirstTag>
</TagOne>;</FirstTag>

and create a file that contains:

<Name>

<FirstTag>
<ITO>
<Name>FirstElement</Name>
<Test>Hello World</Test>
</ITO>
</FirstTag>

</Name>

But I want to also be able to specify in the replacement list that the <Name> and </Name> tags should be replaced by <ElementName> and </ElementName>. I can't think of a way to get my app to do that advanced a search and replace. Do you know Thomas if Regex can do an advanced search and replace like that. Because in my scenario, I don't want all the <Name> and </Name> tags replaced...only the ones that are in a <TagOne>...</TagOne> structure. I guess in terms of specifying that in the replacement list, it shouldn't be too hard. I was thinking of using a colon to show it. For example:
<TagOne>:<Name>;<ElementName>
This might not be the best way, but that's a moot point. Please help if you can. Thanks.
Hiran

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Help with replacing parts of a file Posted: Apr 18, 2002 11:32 PM
Reply to this message Reply
Sounds to me like you need to parse the XML, not just do regular expression processing on it, since you are interested in the names of subnodes of particular nodes. So, in this case, you could use SAX, read in the old file and change names based on context while creating an output file, or you could use DOM and read in the whole tree, then go and rename nodes as appropriate and write out the results. In other words, you probably want to use JAXP: http://java.sun.com/xml/jaxp/index.html

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Help with replacing parts of a file Posted: Apr 19, 2002 12:37 AM
Reply to this message Reply
Matt,
I had that felling too first, but then I thought that he may need to have a "ParserCommander" class for each XML. So brute "find / replace" was more appropriated...
You don't have the same feeling apparently ?
Thomas,

Flat View: This topic has 5 replies on 1 page
Topic: paint() Previous Topic   Next Topic Topic: How to run .chm file from Java code?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use