package kr.co.kihyun.text.hml;

import java.util.Vector;

public class Hwp97 {
	public static final int INITIAL_SIZE = 0;

	private static int POSX = 0;
	private static int POSY = 0;
	private static int VALUE1 = 0;
	private static int VALUE2 = 0;

	public Hwp97() {
	}

	public static String getCODE(String s) {
		StringBuffer sb = null;
		StringBuffer html = null;

		String htm_opened = "";

		String xml_heading = "";
		String xml_body = "";
		String xml_ending = "";

		int offset = 0;

		int opener_idx1 = 0;
		int closer_idx1 = 0;

		int opener_idx2 = 0;
		int closer_idx2 = 0;

		html = new StringBuffer(INITIAL_SIZE);
		html.append(s);

		htm_opened = s;

		opener_idx1 = html.indexOf("<!--");
		closer_idx1 = html.indexOf("-->");
		opener_idx2 = html.lastIndexOf("<!--");
		closer_idx2 = html.lastIndexOf("-->");

		sb = new StringBuffer(INITIAL_SIZE);

		if (opener_idx1 != -1) {
			xml_heading = html.substring(opener_idx1 + 4, closer_idx1);
			sb.append(xml_heading);

			int inner_table_serch_idx = 0;
			int inner_table_serch_lidx = 0;

			// HWP97 ////////////////////////////
			int RowCount[] = new int[10000];
			Vector<Integer> v = new Vector<Integer>();
			boolean INIT_DOING = true;
			// /////////////////////////////////////

			String INNER_TABLE[] = new String[10000];

			// HWP97 ///////////////////////////////
			String TABLE[] = new String[10000];
			// /////////////////////////////////////////

			int i = 0;

			while (true) {

				inner_table_serch_idx = htm_opened.indexOf("<INNER><TABLE", inner_table_serch_idx);

				if (inner_table_serch_idx != -1) {

					inner_table_serch_lidx = htm_opened.indexOf("</INNER>", inner_table_serch_idx);
					INNER_TABLE[i] = htm_opened.substring(inner_table_serch_idx + 7, inner_table_serch_lidx);

					htm_opened = htm_opened.replaceFirst(INNER_TABLE[i], "VIRTUAL DATA" + i);

					// HWP97
					// ///////////////////////////////////////////////////////////////////////////////////////////////
					TABLE[i] = makeTD(INNER_TABLE[i], TableTagConverter.getColCount(INNER_TABLE[i]));
					RowCount[i] = TableTagConverter.getRowCount(TABLE[i]);
					v.addElement(new Integer(RowCount[i]));
					// /////////////////////////////////////////////////////////////////////////////////////////////////////////

					inner_table_serch_idx++;
					i++;

				} else
					break;

			}

			htm_opened = htm_opened.replaceAll("<tr><td></td></tr>", "");

			xml_body = hwp97_COVERTING(htm_opened, v, INIT_DOING);

			String xml_body_array[] = new String[10000];

			for (int j = 0; j < i; j++) {
				xml_body_array[j] = hwp97_COVERTING(TABLE[j], null, false);
				xml_body = xml_body.replaceFirst("VIRTUAL DATA" + j, xml_body_array[j]);
			}

			// HWP97 크기 재조정 --------------------------------------------------------------
			xml_body = xml_body.replaceAll("HWIDTH=x0xx", "HWIDTH=" + VALUE1);
			// -------------------------------------------------------------------------------------

			// HWP97 정렬 태그 위치조정 --------------------------------------------------------------
			xml_body = xml_body.replaceAll("<TEXT><PSTYLE ALIGN=LEFT>", "<PSTYLE ALIGN=LEFT><TEXT>");
			xml_body = xml_body.replaceAll("<TEXT><PSTYLE ALIGN=CENTER>", "<PSTYLE ALIGN=CENTER><TEXT>");
			xml_body = xml_body.replaceAll("<TEXT><PSTYLE ALIGN=RIGHT>", "<PSTYLE ALIGN=RIGHT><TEXT>");
			// -------------------------------------------------------------------------------------

			sb.append(xml_body);

			xml_ending = html.substring(opener_idx2 + 4, closer_idx2);
			sb.append(xml_ending);

		} else {
			sb.append(html.substring(offset, html.length()));
		}

		if (closer_idx2 == -1) {
			offset = closer_idx2 + 1;
		}

		return sb.toString();

	}

	public static String hwp97_COVERTING(String a, Vector<Integer> vector, boolean oo) {

		StringBuffer s = new StringBuffer(INITIAL_SIZE);

		Vector<Integer> v = vector;

		boolean INIT_DOING = oo;

		String html = a;
		String htm_table_opened = "";
		String htm_td_opened = "";
		String htm_td_LOADING = "";
		String htm_table_closed = "";

		String hml_body = "";

		String hml_cell_SAVING = "";
		String hml_cell_LOADING = "";
		String hml_cell_SAVED = "";
		String hml_table_closed = "";

		int tag_table_idx = 0;
		int tag_tr_idx = 0;
		int tag_td_idx = 0;
		// int tag_td_lidx = 0;
		int tag_tr_lidx = 0;
		int tag_table_lidx = 0;

		int hml_cell_idx = 0;
		int hml_cell_lidx = 0;
		int hml_p_lidx = 0;

		String DATA = "";

		int data_serch_idx = 0;

		html = html.replaceAll("</table><TABLE border=1 Width=100 Height=100>", "");

		while (true) {

			htm_table_opened = "";

			// 테이블 open -----------------------------------------------------------------------
			tag_table_idx = html.indexOf("<TABLE", tag_table_idx);

			if (tag_table_idx == -1)
				break;

			tag_table_lidx = html.indexOf("</table>", tag_table_idx);

			htm_table_opened = html.substring(tag_table_idx, tag_table_lidx + 8);

			if (INIT_DOING) {

				Hwp97 set1 = new Hwp97();
				set1.value2();

				int HHEIGHT = Hwp97.VALUE2;
				int HHEIGHT_defaultValue = HHEIGHT;

				// Default Value //////////////////////////
				int HPOSX = 0;
				int HPOSY = 0;
				// ///////////////////////////////////////////

				htm_table_opened = htm_table_opened.replaceAll("<td><INNER>", "<VIRTUAL TAG>");
				htm_table_opened = htm_table_opened.replaceAll("</INNER></td>", "</VIRTUAL TAG>");
				htm_table_opened = htm_table_opened.replaceAll("<tr><td><P>", "<P>");
				htm_table_opened = htm_table_opened.replaceAll("</P></td></tr>", "</P>");
				htm_table_opened = htm_table_opened.replaceAll("</td>", "</TEXT></P></CELL>");

				int Tr_idx = 0;
				int Tr_lidx = 0;

				String VIRTUAL_STRING1 = "";
				String VIRTUAL_STRING2 = "";

				String CELL = "";

				int i = 0;

				while (true) {

					Tr_idx = htm_table_opened.indexOf("<tr>", Tr_idx);

					if (Tr_idx != -1) {

						Tr_lidx = htm_table_opened.indexOf("</tr>", Tr_idx);

						VIRTUAL_STRING1 = htm_table_opened.substring(Tr_idx + 4, Tr_lidx);
						VIRTUAL_STRING2 = htm_table_opened.substring(Tr_idx + 4, Tr_lidx);

						while (true) {

							tag_td_idx = VIRTUAL_STRING2.indexOf("<td>", tag_td_idx);

							if (tag_td_idx != -1) {

								if (v != null) {

									Integer ROWSPAN = (Integer) v.elementAt(i);

									HHEIGHT = HHEIGHT_defaultValue * ROWSPAN.intValue();

									CELL = "<CELL HPOSX=" + HPOSX + " HPOSY=" + HPOSY + " HWIDTH=x0xx HHEIGHT="
											+ HHEIGHT + " ROWSPAN=" + (Integer) v.elementAt(i) + "><P><TEXT>";
									VIRTUAL_STRING2 = VIRTUAL_STRING2.replaceFirst("<td>", CELL);

								} else {
								}

								tag_td_idx++;

							} else
								break;

						}

						htm_table_opened = htm_table_opened.replaceFirst(VIRTUAL_STRING1, VIRTUAL_STRING2);

						i++;

						Tr_idx++;

					} else
						break;

				}

				htm_table_opened = htm_table_opened.replaceAll("<TABLE border=2 Width=100 Height=100>", "");
				htm_table_opened = htm_table_opened.replaceAll("<tr>", "<P><CTRLCODE ID=BOX><BOX TYPE=TABLE>");
				htm_table_opened = htm_table_opened.replaceAll("</tr>", "</CTRLCODE></P>");
				htm_table_opened = htm_table_opened.replaceAll("</table>", "");
				htm_table_opened = htm_table_opened.replaceAll("<VIRTUAL TAG>", "");
				htm_table_opened = htm_table_opened.replaceAll("</VIRTUAL TAG>", "");

			} else {

				int RowCount = TableTagConverter.getRowCount(htm_table_opened);
				int ColCount = TableTagConverter.getColCount(htm_table_opened);

				// htm /td태그를 hml /cell태그로 replace ----------------------------------------
				htm_table_opened = htm_table_opened.replaceAll("</td>", "</CELL>");
				// ------------------------------------------------------------------------------------

				// htm td태그를 hml cell태그로 replace
				// ------------------------------------------------------------------------------------
				int xx = htm_table_opened.indexOf("colspan");
				int yy = htm_table_opened.indexOf("rowspan");

				if (xx != -1 || yy != -1) {

					hml_cell_LOADING = hml_cell_Default_LOADING(RowCount, ColCount);
					hml_cell_SAVING = hml_SPANING(htm_table_opened, hml_cell_LOADING, ColCount); // row_span / col_span
				} else {

					hml_cell_SAVING = hml_cell_Default_LOADING(RowCount, ColCount); // multi_col / multi_row

				}

				while (true) {
					tag_tr_idx = htm_table_opened.indexOf("<tr>", tag_tr_idx);

					if (tag_tr_idx == -1)
						break;

					tag_tr_lidx = htm_table_opened.indexOf("</tr>", tag_tr_idx);
					htm_td_opened = htm_table_opened.substring(tag_tr_idx + 4, tag_tr_lidx);

					while (true) {
						tag_td_idx = htm_td_opened.indexOf("<td", tag_td_idx);
						if (tag_td_idx != -1) {
							data_serch_idx = htm_td_opened.indexOf(">", tag_td_idx);
							hml_cell_lidx = htm_td_opened.indexOf("</CELL>", data_serch_idx + 1);
							DATA = htm_td_opened.substring(data_serch_idx + 1, hml_cell_lidx);
							htm_td_LOADING = htm_td_opened.substring(tag_td_idx, data_serch_idx + 1) + DATA;
							hml_cell_idx = hml_cell_SAVING.indexOf("<CELL", hml_cell_idx);
							if (hml_cell_idx == -1)
								break;
							int data_serch_idx2 = hml_cell_SAVING.indexOf("<TEXT>", hml_cell_idx);
							int hml_text_lidx = hml_cell_SAVING.indexOf("</TEXT>", data_serch_idx2);
							String DATA2 = DATA;
							hml_p_lidx = hml_cell_SAVING.indexOf("</P>", hml_cell_idx);
							hml_cell_SAVED = hml_cell_SAVING.substring(hml_cell_idx, data_serch_idx2 + 6) + DATA2
									+ hml_cell_SAVING.substring(hml_text_lidx, hml_p_lidx + 4);
							htm_table_opened = htm_table_opened.replaceFirst(htm_td_LOADING, hml_cell_SAVED);
							tag_td_idx++;
							hml_cell_idx++;
						} else
							break;

					}
					tag_tr_idx++;
				}

				// htm table태그를 없애기
				// ----------------------------------------------------------------------------------------------------
				htm_table_opened = htm_table_opened.replaceAll("<TABLE border=1 Width=100 Height=100>", "");
				// --------------------------------------------------------------------------------------------------------------------------------

				// htm tr태그를 없애기 -------------------------------------------------
				htm_table_opened = htm_table_opened.replaceAll("<tr>", "");
				// -------------------------------------------------------------------------

				// htm /tr태그를 없애기 -------------------------------------------------
				htm_table_opened = htm_table_opened.replaceAll("</tr>", "");
				// --------------------------------------------------------------------------

				// htm /table태그를 없애기 --------------------------------------------------
				htm_table_opened = htm_table_opened.replaceAll("</table>", "");
				// ------------------------------------------------------------------------------

			}

			// 특수코드를 특수문자로 replace ----------------------------------------------------------
                        //46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO  
                        //htm_table_opened = TableTagConverter.CODE_SPECIALING(htm_table_opened);
                        htm_table_opened = TableTagConverter.codeSpecialing(htm_table_opened);
                        //================================================
			// ----------------------------------------------------------------------------------------------

			// 테이블 close ----------------------------------------------------
			htm_table_closed = htm_table_closed + htm_table_opened;
			// -------------------------------------------------------------------

			tag_table_idx++;

		}

		// 디자인 구성
		// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0</TEXT></P><", " LLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>RLINE=0</TEXT></P><", " RLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>BLINE=0</TEXT></P><", " BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 RLINE=0</TEXT></P><",
				" LLINE=0 RLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 TLINE=0</TEXT></P><",
				" LLINE=0 TLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 BLINE=0</TEXT></P><",
				" LLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>RLINE=0 BLINE=0</TEXT></P><",
				" RLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>TLINE=0 BLINE=0</TEXT></P><",
				" TLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 RLINE=0 TLINE=0</TEXT></P><",
				" LLINE=0 RLINE=0 TLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 RLINE=0 BLINE=0</TEXT></P><",
				" LLINE=0 RLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 TLINE=0 BLINE=0</TEXT></P><",
				" LLINE=0 TLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>RLINE=0 TLINE=0 BLINE=0</TEXT></P><",
				" RLINE=0 TLINE=0 BLINE=0><P></P><");
		htm_table_closed = htm_table_closed.replaceAll("><P><TEXT>LLINE=0 RLINE=0 TLINE=0 BLINE=0</TEXT></P><",
				" LLINE=0 RLINE=0 TLINE=0 BLINE=0><P></P><");
		// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		// 97기능판, 2002, 2002SE 상호 호환을 위한 코드/////////////////////////////////////////////////////
		String htm_table_Completely_disappeared = htm_table_closed;
		htm_table_Completely_disappeared = htm_table_Completely_disappeared.replace('\n', ' ');
		// //////////////////////////////////////////////////////////////////////////////////////////////////////////

		hml_table_closed = hml_table_closed + htm_table_Completely_disappeared;
		hml_body = hml_table_closed;
		s.append(hml_body);

		return s.toString();

	}

	// TD 생성기--------------------------------------------------------------------------

	public static String makeTD(String a, int b) {

		StringBuffer s = new StringBuffer(INITIAL_SIZE);

		int t = 0;
		int u = 0;
		int c = 0;

		int nt = 0;

		int r = 0;
		int q = 0;

		String T = "";
		String R = "";

		while (true) {
			t = a.indexOf("<TABLE", t);
			nt = a.indexOf("<TABLE", t + 6);

			if (t == -1)
				break;

			u = a.indexOf("</table>", t);
			T = a.substring(t, u);

			c = TableTagConverter.getColCount(T);

			if (b == c && c != 1) {
				s.append(T);
				if (nt > -1) {
					s.append("<tr>");
					s.append("<td>RLINE=0</td>");
					for (int j = 0; j < b - 2; j++) {
						s.append("<td>LLINE=0 RLINE=0</td>");
					}
					s.append("<td>LLINE=0</td>");
					s.append("</tr>");
				}

			} else if (b > c) {
				boolean TRUE = true;

				s.append("<TABLE border=1 Width=100 Height=100>");
				while (true) {
					r = T.indexOf("<tr>", r);

					if (r == -1)
						break;

					q = T.indexOf("</tr>", r);
					R = T.substring(r, q);

					if ((b - c) != 1) {

						if (TRUE) {
							s.append(R);
							s.append("<td>RLINE=0 BLINE=0</td>");
							for (int j = 0; j < b - c - 2; j++) {
								s.append("<td>LLINE=0 RLINE=0 BLINE=0</td>");
							}
							s.append("<td>LLINE=0 BLINE=0</td>");
							s.append("</tr>");
						} else {
							s.append(R);
							s.append("<td>RLINE=0 TLINE=0 BLINE=0</td>");
							for (int j = 0; j < b - c - 2; j++) {
								s.append("<td>LLINE=0 RLINE=0 TLINE=0 BLINE=0</td>");
							}
							s.append("<td>LLINE=0 TLINE=0 BLINE=0</td>");
							s.append("</tr>");
						}

					} else {

						if (TRUE) {
							s.append(R);
							s.append("<td>BLINE=0</td>");
							s.append("</tr>");
						} else {
							s.append(R);
							s.append("<td>TLINE=0 BLINE=0</td>");
							s.append("</tr>");
						}

					}
					r++;
					TRUE = false;

				}

				if (nt > -1) {
					s.append("<tr>");
					s.append("<td>RLINE=0</td>");
					for (int j = 0; j < c - 1; j++) {
						s.append("<td>LLINE=0 RLINE=0</td>");
					}
					for (int j = 0; j < b - c - 1; j++) {
						s.append("<td>LLINE=0 RLINE=0 TLINE=0</td>");
					}
					s.append("<td>LLINE=0 TLINE=0</td>");
					s.append("</tr>");
				}
			} else {
				s.append(T);
			}

			s.append("</table>");
			t++;
		}

		return s.toString();

	}

	// hml cell Default LOADER
	// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

	public static String hml_cell_Default_LOADING(int a, int b) {

		StringBuffer s = new StringBuffer(INITIAL_SIZE);

		String DATA = "";

		int RowCount = a;
		int ColCount = b;
		int DefaultWidth = 0;
		int DefaultHeight = 0;

		// 크기조정 /////////////////////
		Hwp97 set2 = new Hwp97();
		set2.value1(b);
		set2.value2();
		DefaultWidth = Hwp97.VALUE1;
		DefaultHeight = Hwp97.VALUE2;
		// ////////////////////////////////

		// 한글97 X,Y, 초기화 ////////
		Hwp97 Hx = new Hwp97();
		Hx.setPOSX(DefaultWidth);
		Hwp97 Hy = new Hwp97();
		Hy.setPOSY(DefaultHeight);
		// //////////////////////////////

		for (int i = 0; i < RowCount; i++) {

			for (int j = 0; j < ColCount; j++) {

				s.append("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight) + " HWIDTH="
						+ DefaultWidth + " HHEIGHT=" + DefaultHeight + " COLSPAN=1 ROWSPAN=1><P><TEXT>");

				DATA = "          "; // DATA 값 초기화
				s.append(DATA);

				s.append("</TEXT></P>\n");

			}

		}

		return s.toString();

	}

	// SPANING
	// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

	public static String hml_SPANING(String a, String b, int CC) {
		StringBuffer s = new StringBuffer(INITIAL_SIZE);

		String v = "";
		String v1 = "";
		String v2 = "";
		String v3 = "";
		String v4 = "";
		String v5 = "";
		String v6 = "";
		String v7 = "";
		String v8 = "";

		String e = "";
		String e1 = "";
		String e2 = "";
		String e3 = "";

		String J_init1 = "";
		String J_init2 = "";
		String J_init3 = "";
		String J_init4 = "";

		int J_init_idx1 = 0;
		int J_init_idx2 = 0;
		int J_init_idx3 = 0;
		int J_init_idx4 = 0;

		int J_init_lidx1 = 0;
		int J_init_lidx2 = 0;
		int J_init_lidx3 = 0;
		int J_init_lidx4 = 0;

		int DefaultWidth = 0;
		int DefaultHeight = 0;

		int c = 0;
		int c1 = 0;
		int c2 = 0;
		int c3 = 0;

		int d = 0;
		int d1 = 0;
		int d2 = 0;
		int d3 = 0;

		int i = 0;
		int j = 0;

		int o = 0;
		int o1 = 0;

		int p = 0;
		int p1 = 0;

		int r = 0;
		int u = 0;

		int t = 0;
		int w = 0;

		int x = 0;
		int y = 0;
		int z = 0;

		int x1 = 0;
		int y1 = 0;
		int z1 = 0;

		int y2 = 0;
		int z2 = 0;

		int y3 = 0;
		int z3 = 0;

		int ColAddr = 0;
		int RowAddr = 0;

		int ColAddr2 = 0;
		int RowAddr2 = 0;

		DefaultWidth = Hwp97.getPOSX();
		DefaultHeight = Hwp97.getPOSY();

		TableTagConverter TTC = new TableTagConverter();
		TTC.set_VR_STR(b);

		while (true) {
			r = a.indexOf("<tr>", r);

			v = "";
			v1 = "";
			v2 = "";
			v3 = "";
			v4 = "";
			v5 = "";
			v6 = "";
			v7 = "";
			v8 = "";

			e = "";
			e1 = "";
			e2 = "";
			e3 = "";

			t = 0;

			if (r == -1)
				break;

			u = a.indexOf("</tr>", r);
			v = a.substring(r + 4, u);

			while (true) {
				t = v.indexOf("<td", t);

				if (t != -1) {
					w = v.indexOf("</CELL>", t);
					v1 = v.substring(t, w);

					x = v1.indexOf("colspan");
					x1 = v1.indexOf("rowspan");

					if (x != -1 && x1 == -1) {

						y = v1.indexOf("\"", x);
						z = v1.indexOf("\"", y + 1);
						int ColSpan = Integer.parseInt(v1.substring(y + 1, z));

						b = TableTagConverter.get_VR_STR();

						J_init_idx1 = b.indexOf("<CELL HPOSX=");
						J_init_lidx1 = b.indexOf(" HPOSY", J_init_idx1 + 12);
						J_init1 = b.substring(J_init_idx1 + 12, J_init_lidx1);
						j = (Integer.parseInt(J_init1) / DefaultWidth) - 1;

						e = "<xxxx HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight) + " HWIDTH="
								+ (ColSpan * DefaultWidth) + " HHEIGHT=" + DefaultHeight + " COLSPAN=" + ColSpan;

						c = b.indexOf("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight));
						if (c == -1)
							break;
						d = b.indexOf(" ROWSPAN", c);
						v2 = b.substring(c, d);

						b = b.replaceAll(v2, e);

						for (int q = 1; q < ColSpan; q++) {
							ColAddr = j + q;

							o = b.indexOf("<CELL HPOSX=" + ((ColAddr + 1) * DefaultWidth) + " HPOSY="
									+ (i * DefaultHeight));

							if (o == -1)
								break;
							p = b.indexOf("</P>", o);
							v3 = b.substring(o, p + 4);

							b = b.replaceAll(v3, "");

						} // end for

					} else if (x == -1 && x1 != -1) {
						y1 = v1.indexOf("\"", x1);
						z1 = v1.indexOf("\"", y1 + 1);
						int RowSpan = Integer.parseInt(v1.substring(y1 + 1, z1));

						b = TableTagConverter.get_VR_STR();

						J_init_idx2 = b.indexOf("<CELL HPOSX=");
						J_init_lidx2 = b.indexOf(" HPOSY", J_init_idx2 + 12);
						J_init2 = b.substring(J_init_idx2 + 12, J_init_lidx2);
						j = (Integer.parseInt(J_init2) / DefaultWidth) - 1;

						e1 = "<xxxx HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight) + " HWIDTH="
								+ DefaultWidth + " HHEIGHT=" + (RowSpan * DefaultHeight) + " COLSPAN=1 ROWSPAN="
								+ RowSpan;

						c1 = b.indexOf("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight));
						if (c1 == -1)
							break;
						d1 = b.indexOf(">", c1);
						v4 = b.substring(c1, d1);

						b = b.replaceAll(v4, e1);

						for (int q = 1; q < RowSpan; q++) {

							RowAddr = i + q;

							o1 = b.indexOf("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY="
									+ (RowAddr * DefaultHeight));

							if (o1 == -1)
								break;
							p1 = b.indexOf("</P>", o1);
							v5 = b.substring(o1, p1 + 4);

							b = b.replaceAll(v5, "");

						} // end for

					} else if (x != -1 && x1 != -1) {
						y2 = v1.indexOf("\"", x);
						z2 = v1.indexOf("\"", y2 + 1);
						int ColSpan2 = Integer.parseInt(v1.substring(y2 + 1, z2));

						y3 = v1.indexOf("\"", x1);
						z3 = v1.indexOf("\"", y3 + 1);
						int RowSpan2 = Integer.parseInt(v1.substring(y3 + 1, z3));

						b = TableTagConverter.get_VR_STR();

						J_init_idx3 = b.indexOf("<CELL HPOSX=");
						J_init_lidx3 = b.indexOf(" HPOSY", J_init_idx3 + 12);
						J_init3 = b.substring(J_init_idx3 + 12, J_init_lidx3);
						j = (Integer.parseInt(J_init3) / DefaultWidth) - 1;

						e2 = "<xxxx HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight) + " HWIDTH="
								+ (ColSpan2 * DefaultWidth) + " HHEIGHT=" + (RowSpan2 * DefaultHeight) + " COLSPAN="
								+ ColSpan2 + " ROWSPAN=" + RowSpan2;

						c2 = b.indexOf("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight));
						if (c2 == -1)
							break;
						d2 = b.indexOf(">", c2);
						v6 = b.substring(c2, d2);
						b = b.replaceAll(v6, e2);

						for (int q1 = 0; q1 < RowSpan2; q1++) {
							RowAddr2 = i + q1;
							for (int q2 = 0; q2 < ColSpan2; q2++) {
								ColAddr2 = j + q2;
                                                                
                                                                //42.비어있는 if문 : Delete by YOUNGJUN,CHO
                                                                /*
								if (q1 == 0 & q2 == 0) {
								} else {
									int o2 = b.indexOf("<CELL HPOSX=" + ((ColAddr2 + 1) * DefaultWidth) + " HPOSY="
											+ (RowAddr2 * DefaultHeight));
									if (o2 == -1)
										break;
									int p2 = b.indexOf("</P>", o2);
									v7 = b.substring(o2, p2 + 4);

									b = b.replaceAll(v7, "");

								}
                                                                */
                                                                //------------------------------------------------
                                                                
                                                                //42.비어있는 if문 : Add by YOUNGJUN,CHO
                                                                if (q1 != 0 || q2 != 0) {
                                                                    int o2 = b.indexOf("<CELL HPOSX=" + ((ColAddr2 + 1) * DefaultWidth) + " HPOSY=" + (RowAddr2 * DefaultHeight));
                                                                    if (o2 == -1) {
                                                                        break;
                                                                    }
                                                                            
                                                                    int p2 = b.indexOf("</P>", o2);
                                                                    v7 = b.substring(o2, p2 + 4);

                                                                    b = b.replaceAll(v7, "");
								}
                                                                //++++++++++++++++++++++++++++++++++++++++++++++++
                                                                
							} // end inner for

						} // end outer for

					} else {

						b = TableTagConverter.get_VR_STR();

						J_init_idx4 = b.indexOf("<CELL HPOSX=");
						J_init_lidx4 = b.indexOf(" HPOSY", J_init_idx4 + 12);
						J_init4 = b.substring(J_init_idx4 + 12, J_init_lidx4);
						j = (Integer.parseInt(J_init4) / DefaultWidth) - 1;

						e3 = "<xxxx HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight) + " HWIDTH";

						c3 = b.indexOf("<CELL HPOSX=" + ((j + 1) * DefaultWidth) + " HPOSY=" + (i * DefaultHeight));
						if (c3 == -1)
							break;
						d3 = b.indexOf("HWIDTH", c3);
						v8 = b.substring(c3, d3 + 6);

						b = b.replaceAll(v8, e3);

					}

					TTC.set_VR_STR("");
					TTC.set_VR_STR(b);

					t++;

				} else
					break;

			} // end while

			r++;
			i++;

		} // end while
		b = b.replaceAll("<xxxx", "<CELL"); // 셀병합 최종 마무리
		s.append(b);

		return s.toString();

	}

	public void setPOSX(int X) {
		this.POSX = X;
	}

	public static int getPOSX() {
		return POSX;
	}

	public void setPOSY(int Y) {
		this.POSY = Y;
	}

	public static int getPOSY() {
		return POSY;
	}

	// 크기조정 control-----------------
        //46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO  
        //public void VALUE1(int Col) {
        public void value1(int Col) {
            if (Col > 3) {
                this.VALUE1 = 10000 / Col;
            } else {
                this.VALUE1 = 5500 / Col;
            }
	}
        //================================================
        
        //46.메소드이름 생성규칙 위반 Update by YOUNGJUN,CHO  
        //public void VALUE2() {
        public void value2() {
            this.VALUE2 = 100;
	}
        //================================================
}
