001package utility;
002
003import org.apache.commons.io.FileUtils;
004import org.junit.After;
005import org.junit.Before;
006import org.junit.Test;
007import org.kuali.ole.docstore.xstream.BaseTestCase;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011import java.io.File;
012import java.net.URL;
013import java.util.regex.Matcher;
014import java.util.regex.Pattern;
015
016/**
017 * Created by IntelliJ IDEA.
018 * User: Sreekanth
019 * Date: 6/4/12
020 * Time: 11:21 AM
021 * To change this template use File | Settings | File Templates.
022 */
023public class XMLUtility_UT
024        extends BaseTestCase {
025    private static final Logger LOG = LoggerFactory.getLogger(XMLUtility_UT.class);
026
027
028    @Before
029    public void setUp() throws Exception {
030    }
031
032    @After
033    public void tearDown() throws Exception {
034        //To change body of created methods use File | Settings | File Templates.
035    }
036
037    @Test
038    public void testAllTextUsingRegEx() throws Exception {
039        String filePath = "/org/kuali/ole/utility/license/OLE-License-ONIXPL.xml";
040        URL resource = getClass().getResource(filePath);
041        File file = new File(resource.toURI());
042        String sampleText = FileUtils.readFileToString(file);
043        String[] content = sampleText.split("content>");
044        for (int i = 0; i < content.length; i++) {
045            if (i % 2 == 0) {
046                if (content[i].contains("additionalAttributes")) {
047                    LOG.debug("additionalAttributes");
048                    LOG.debug("======================");
049                    getText(content[i]);
050                }
051            } else {
052
053                LOG.debug("content");
054                LOG.debug("======================");
055                getText(content[i]);
056            }
057
058        }
059
060    }
061
062    private void getText(String input) {
063        String reg = "<.*>(.*)<\\/.*?>";
064        Pattern p = Pattern.compile(reg);
065        Matcher m = p.matcher(input);
066
067        while (m.find()) {
068            String s1 = m.group(1);
069            LOG.debug(s1);
070        }
071    }
072
073}
074