Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Methodology</h2> <p>A solution to implement is the following:</p> <ul> <li>create an in memory 2D array (array of arrays) which is similar to a chessboard.</li> </ul> <p>Then i will create an algorith that when it detects "-" characters, i initialize acall to a method to detect the remaining corners ( top right, bottom left, bottom right) following the characters and where they end.</p> <p>Example ( quick pseudocode ):</p> <p><code>while(selectedCell==I) selectedCell=selectedCell.goDown();</code></p> <p>Using such a strategy you can map out your boxes and which boxes are contained within which.</p> <p>Remaining would be to print this info as html..</p> <h2>Quick and Dirty Implementation</h2> <p>Since I was in the mood I spent an hour+ to quickly cook up a toy implementation. The below is non-optimized in respect to that I do not make use of Iterators to go over Cells, and would need refactoring to become a serious framework.</p> <p><strong>Cell.java</strong></p> <hr> <pre><code>package AsciiToDIVs; public class Cell { public char Character; public CellGrid parentGrid; private int rowIndex; private int colIndex; public Cell(char Character, CellGrid parent, int rowIndex, int colIndex) { this.Character = Character; this.parentGrid = parent; this.rowIndex = rowIndex; this.colIndex = colIndex; } public int getRowIndex() { return rowIndex; } public int getColIndex() { return colIndex; } } </code></pre> <p><strong>CellGrid.java</strong></p> <hr> <pre><code>package AsciiToDIVs; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; public class CellGrid { private ArrayList&lt;ArrayList&lt;Cell&gt;&gt; CellGridData; public CellGrid(String asciiFile) throws IOException { readDataFile(asciiFile); } public ArrayList&lt;FoundObject&gt; findBoxes(FoundBoxObject parent) { int startRowIndex = 0, startColIndex = 0, parentRowLimit = Integer.MAX_VALUE, parentColLimit = Integer.MAX_VALUE, startingColIndex = 0; if(parent != null) { startRowIndex = parent.getRowIndex()+1; startColIndex = startingColIndex = parent.getColIndex()+1; parentRowLimit = parent.getRowIndex() + parent.getHeight(); parentColLimit = parent.getColIndex() + parent.getWidth(); } ArrayList&lt;FoundObject&gt; results = new ArrayList&lt;FoundObject&gt;(); Cell currentCell; if(startRowIndex&gt;=CellGridData.size()) return null; for(; startRowIndex&lt;CellGridData.size() &amp;&amp; startRowIndex&lt;parentRowLimit; startRowIndex++ ) { startColIndex = startingColIndex; for(; startColIndex&lt; CellGridData.get(startRowIndex).size() &amp;&amp; startColIndex&lt;parentColLimit; startColIndex++) { FoundBoxObject withinBox = checkWithinFoundBoxObject(results, startRowIndex, startColIndex); if(withinBox !=null) startColIndex+=withinBox.getWidth(); currentCell = getCell(startRowIndex, startColIndex); if(currentCell!=null) { if(currentCell.Character == '-') // Found a TOP-CORNER { int boxHeight = getConsecutiveIs(startRowIndex+1, startColIndex) + 1; if(boxHeight&gt;1) { int boxWidth = getConsecutiveDashes(startRowIndex, startColIndex); FoundBoxObject box = new FoundBoxObject(startRowIndex, startColIndex, boxWidth, boxHeight, parent); results.add(box); findBoxes(box); startColIndex+=boxWidth; } } //This is a character else if(currentCell.Character != '-' &amp;&amp; currentCell.Character != 'I' &amp;&amp; currentCell.Character != ' ' &amp;&amp; currentCell.Character != '\n' &amp;&amp; currentCell.Character != '\n' &amp;&amp; currentCell.Character != '\t') { FoundCharObject Char = new FoundCharObject(startRowIndex, startColIndex, parent, currentCell.Character); results.add(Char); } } } } if(parent!=null) parent.containedObjects = results; return results; } public static String printDIV(ArrayList&lt;FoundObject&gt; objects) { String result = ""; Iterator&lt;FoundObject&gt; it = objects.iterator(); FoundObject fo; while(it.hasNext()) { result+="&lt;div&gt;"; fo = it.next(); if(fo instanceof FoundCharObject) { FoundCharObject fc = (FoundCharObject)fo; result+=fc.getChar(); } if(fo instanceof FoundBoxObject) { FoundBoxObject fb = (FoundBoxObject)fo; result+=printDIV(fb.containedObjects); } result+="&lt;/div&gt;"; } return result; } private FoundBoxObject checkWithinFoundBoxObject(ArrayList&lt;FoundObject&gt; results, int rowIndex, int colIndex) { Iterator&lt;FoundObject&gt; it = results.iterator(); FoundObject f; FoundBoxObject fbox = null; while(it.hasNext()) { f = it.next(); if(f instanceof FoundBoxObject) { fbox = (FoundBoxObject) f; if(rowIndex &gt;= fbox.getRowIndex() &amp;&amp; rowIndex &lt;= fbox.getRowIndex() + fbox.getHeight()) { if(colIndex &gt;= fbox.getColIndex() &amp;&amp; colIndex &lt;= fbox.getColIndex() + fbox.getWidth()) { return fbox; } } } } return null; } private int getConsecutiveDashes(int startRowIndex, int startColIndex) { int counter = 0; Cell cell = getCell(startRowIndex, startColIndex); while( cell!=null &amp;&amp; cell.Character =='-') { counter++; cell = getCell(startRowIndex, startColIndex++); } return counter; } private int getConsecutiveIs(int startRowIndex, int startColIndex) { int counter = 0; Cell cell = getCell(startRowIndex, startColIndex); while( cell!=null &amp;&amp; cell.Character =='I') { counter++; cell = getCell(startRowIndex++, startColIndex); } return counter; } public Cell getCell(int rowIndex, int columnIndex) { ArrayList&lt;Cell&gt; row; if(rowIndex&lt;CellGridData.size()) row = CellGridData.get(rowIndex); else return null; Cell cell = null; if(row!=null){ if(columnIndex&lt;row.size()) cell = row.get(columnIndex); } return cell; } public Iterator&lt;ArrayList&lt;Cell&gt;&gt; getRowGridIterator(int StartRow) { Iterator&lt;ArrayList&lt;Cell&gt;&gt; itRow = CellGridData.iterator(); int CurrentRow = 0; while (itRow.hasNext()) { // Itrate to Row if (CurrentRow++ &lt; StartRow) itRow.next(); } return itRow; } private void readDataFile(String asciiFile) throws IOException { CellGridData = new ArrayList&lt;ArrayList&lt;Cell&gt;&gt;(); ArrayList&lt;Cell&gt; row; FileInputStream fstream = new FileInputStream(asciiFile); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine; // Read File Line By Line int rowIndex = 0; while ((strLine = br.readLine()) != null) { CellGridData.add(row = new ArrayList&lt;Cell&gt;()); // System.out.println (strLine); for (int colIndex = 0; colIndex &lt; strLine.length(); colIndex++) { row.add(new Cell(strLine.charAt(colIndex), this, rowIndex,colIndex)); // System.out.print(strLine.charAt(i)); } rowIndex++; // System.out.println(); } // Close the input stream br.close(); } public String printGrid() { String result = ""; Iterator&lt;ArrayList&lt;Cell&gt;&gt; itRow = CellGridData.iterator(); Iterator&lt;Cell&gt; itCol; Cell cell; while (itRow.hasNext()) { itCol = itRow.next().iterator(); while (itCol.hasNext()) { cell = itCol.next(); result += cell.Character; } result += "\n"; } return result; } } </code></pre> <p><strong>FoundBoxObject.java</strong></p> <hr> <pre><code>package AsciiToDIVs; import java.util.ArrayList; public class FoundBoxObject extends FoundObject { public ArrayList&lt;FoundObject&gt; containedObjects = new ArrayList&lt;FoundObject&gt;(); public static int boxCounter = 0; public final int ID = boxCounter++; public FoundBoxObject(int rowIndex, int colIndex, int width, int height, FoundBoxObject parent) { super(rowIndex, colIndex, width, height); if(parent!=null) System.out.println("Created a box(" + "ID="+ID+ ",X="+rowIndex+ ",Y="+colIndex+ ",width="+width+ ",height="+height+ ",parent="+parent.ID+")"); else System.out.println("Created a box(" + "ID="+ID+ ",X="+rowIndex+ ",Y="+colIndex+ ",width="+width+ ",height="+height+ ")"); } } </code></pre> <p><strong>FoundCharObject.java</strong></p> <hr> <pre><code>package AsciiToDIVs; public class FoundCharObject extends FoundObject { private Character Char; public FoundCharObject(int rowIndex, int colIndex,FoundBoxObject parent, char Char) { super(rowIndex, colIndex, 1, 1); if(parent!=null) System.out.println("Created a char(" + "Char="+Char+ ",X="+rowIndex+ ",Y="+colIndex+ ",parent="+parent.ID+")"); else System.out.println("Created a char(" + ",X="+rowIndex+ ",Y="+colIndex+")"); this.Char = Char; } public Character getChar() { return Char; } } </code></pre> <p><strong>FoundObject.java</strong></p> <hr> <pre><code>package AsciiToDIVs; public class FoundObject { private int rowIndex; private int colIndex; private int width = 0; private int height = 0; public FoundObject(int rowIndex, int colIndex, int width, int height ) { this.rowIndex = rowIndex; this.colIndex = colIndex; this.width = width; this.height = height; } public int getRowIndex() { return rowIndex; } public int getColIndex() { return colIndex; } public int getWidth() { return width; } public int getHeight() { return height; } } </code></pre> <p><strong>Main Method</strong></p> <hr> <pre><code>public static void main(String args[]) { try { CellGrid grid = new CellGrid("ascii.txt"); System.out.println(CellGrid.printDIV(grid.findBoxes(null))); //System.out.println(grid.printGrid()); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <h2>Update</h2> <p>The 'printDIV' should be like this (more '' were being printed than needed).</p> <pre><code>public static String printDIV(ArrayList&lt;FoundObject&gt; objects) { String result = ""; Iterator&lt;FoundObject&gt; it = objects.iterator(); FoundObject fo; while(it.hasNext()) { fo = it.next(); if(fo instanceof FoundCharObject) { FoundCharObject fc = (FoundCharObject)fo; result+=fc.getChar(); } if(fo instanceof FoundBoxObject) { result+="&lt;div&gt;"; FoundBoxObject fb = (FoundBoxObject)fo; result+=printDIV(fb.containedObjects); result+="&lt;/div&gt;"; } } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload