001/*
002 *   StartSection  -- Encapsulates the IGES Start Section.
003 *
004 *   Copyright (C) 2010-2016, Joseph A. Huwaldt.
005 *   All rights reserved.
006 *   
007 *   This library is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *   
012 *   This library is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 *   Lesser General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public License
018 *   along with this program; if not, write to the Free Software
019 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
021 *
022 *   Based on, but heavily modified from, IGESView ( http://ts.nist.gov/Standards/IGES/igesTools.cfm )
023 */
024package geomss.geom.reader.iges;
025
026import java.io.IOException;
027import java.io.PrintWriter;
028import java.io.RandomAccessFile;
029import java.util.regex.*;
030
031/**
032 * The StartSection class encapsulates the IGES Start Section. There are methods to
033 * initialize, read, and dump the object into a String.
034 * 
035 * <p> Modified by: Joseph A. Huwaldt </p>
036 * 
037 * @author JDN, Version 1.0
038 * @version September 13, 2016
039 */
040public class StartSection {
041
042    private String StartText = "";
043
044    /**
045     * Default constructor.
046     */
047    public StartSection() {
048    }
049
050    /**
051     * Initialization constructor.
052     *
053     * @param s input string
054     */
055    public StartSection(String s) {
056        StartText = s;
057    }
058
059    /**
060     * Read Start Section from input file. This method keeps track of how many lines are
061     * read in.
062     *
063     * @param in input file
064     * @throws IOException if there is any problem reading the start section.
065     */
066    public void read(RandomAccessFile in) throws IOException {
067        StringBuilder outStr = new StringBuilder();
068
069        while (true) {
070            long curloc = in.getFilePointer();
071            String line = Constants.myReadLine(in);
072            if (line.charAt(72) == 'S') {
073                outStr.append(line.substring(0, Constants.SECTID_COL));
074                outStr.append("\n");
075            } else {
076                in.seek(curloc);
077                break;
078            }
079        }
080
081        StartText = outStr.toString().trim();
082    }
083
084    /**
085     * Write the Start Section to the specified writer.
086     *
087     * @param writer The PrintWriter to write the Start Section to.
088     * @return The number of lines written out to the start section.
089     * @throws IOException if there is any problem writing the start section.
090     */
091    public int write(PrintWriter writer) throws IOException {
092        String[] lines = StartText.split("\n");
093
094        // This pattern matches control characters
095        Pattern pattern = Pattern.compile("\\{cntrl\\}");
096
097        int indx = 1;
098        int numLines = lines.length;
099        for (int i = 0; i < numLines; ++i) {
100            String line = lines[i].trim();
101            line = removeControlChars(pattern, line);
102            indx = Constants.writeSection(writer, indx, "", 'S', new StringBuilder(line));
103        }
104
105        return indx - 1;
106    }
107
108    /**
109     * Remove any ASCII control characters from the specified String.
110     */
111    private String removeControlChars(Pattern cntrlPattern, String string) {
112        if (string.length() == 0)
113            return string;
114
115        Matcher m = cntrlPattern.matcher(string);
116
117        //Replaces control characters with an empty string.
118        String result = m.replaceAll("");
119
120        return result;
121    }
122
123    /**
124     * Set Start Section string.
125     *
126     * @param s input string
127     */
128    public void setStartSection(String s) {
129        StartText = s;
130    }
131
132    /**
133     * Dump to a String.
134     *
135     * @return result string
136     */
137    @Override
138    public String toString() {
139        return StartText;
140    }
141
142}