Java Programming Advance Capstone Case Study

profilejvlieti4440
CaseStudy.pdf

/****************************** Module Header ******************************\ 1 * Copyright (c) Microsoft Corporation 2 3 * This source is subject to the Microsoft Public License. 4 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 5 * All other rights reserved. 6 * 7 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 8 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 9 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 10 \*****************************************************************************/ 11 12 13 14 using System; 15 using System.Collections; 16 using System.Configuration; 17 using System.Data; 18 using System.Linq; 19 using System.Web; 20 using System.Web.Security; 21 using System.Web.UI; 22 using System.Web.UI.HtmlControls; 23 using System.Web.UI.WebControls; 24 using System.Web.UI.WebControls.WebParts; 25 using System.IO; 26 using System.Collections.Generic; 27 28 namespace CSASPNETIntelligentTextBox 29 { 30 public partial class Default : System.Web.UI.Page 31 { 32 // Define some public variables for load word dictionary event. 33 static ArrayList LineList = new ArrayList(); 34 public int StringNumber; 35 protected void Page_Load(object sender, EventArgs e) 36 { 37 #region ////Load Method//// 38 39 LineList.Clear(); 40 // Use StreamReader to load word dictionary , store them in an ArrayList. 41 string dictionaryFilePath = Page.Server.MapPath("~/Dictionary/WordList.txt"); 42 using (StreamReader fileStream = new StreamReader(dictionaryFilePath)) 43 { 44 if (fileStream == null) 45 { 46 return; 47 } 48 string readLine = string.Empty; 49 StringNumber = 0; 50 while (readLine != null) 51 { 52 readLine = fileStream.ReadLine(); 53 if (!String.IsNullOrEmpty(readLine)) 54 { 55 LineList.Add(readLine.Replace("'", "\'")); 56 StringNumber++; 57 } 58 } 59 } 60 StringNumber = LineList.Count; 61 62 #endregion 63 } 64 65 /// <summary> 66 /// This method is used to call check user's input word and returns some recommended words.67 68 /// A JavsScript function will invoke this method to execute server-side code, 69 /// This will bring many benefits, such as when user input words and the application will 70

/// update recommended word list without page refresh, it can provide more responsive. 71 /// </summary> 72 /// <param name="key"></param> 73 /// <returns></returns> 74 [System.Web.Services.WebMethod] 75 public static string ReturnHtmlString(string key) 76 { 77 #region ////Match Method//// 78 79 // Define an ArrayList to retrieve word list, 80 // the Dictionary use to record similar words. 81 ArrayList list = (ArrayList)LineList; 82 Dictionary<int, string[]> matchEntity = new Dictionary<int, string[]>(); 83 int sortID = 0; 84 int highLevel = 0; 85 // Loop the word dictionary, compare with source word. 86 for (int i = 0; i < list.Count; i++) 87 { 88 // Confirm the word length, calculate word's similar level and 89 // move the cursor to next character. 90 string source = list[i].ToString(); 91 int sourceLength = list[i].ToString().Length; 92 int keyLength = key.Length; 93 int matchLength = ((sourceLength > keyLength) ? keyLength : sourceLength); 94 int matchLevel = 0; 95 int cursor = 0; 96 while (cursor < matchLength) 97 { 98 if (source[cursor] == key[cursor]) 99 { 100 matchLevel++; 101 cursor++; 102 } 103 else if (matchLevel >= 1) 104 { 105 cursor++; 106 } 107 else 108 { 109 matchLevel = 0; 110 break; 111 } 112 } 113 if (matchLevel >= 1) 114 { 115 string[] entity = new string[4]; 116 entity[0] = matchLevel.ToString(); 117 entity[1] = source; 118 entity[2] = sourceLength.ToString(); 119 matchEntity.Add(sortID, entity); 120 if (matchLevel > highLevel) 121 highLevel = matchLevel; 122 sortID++; 123 } 124 } 125 var highLevelList = from d in matchEntity 126 where d.Value[0] == highLevel.ToString() 127 select d; 128 #endregion 129 130 #region ////Sort Method//// 131 132 // Sort the result with the highest similar level and characters' length. 133 // And we must make sure the recommended word list must include 10 words. 134 if (highLevelList.ToList().Count <= 10) 135 { 136 int listNumber = highLevelList.ToList().Count; 137 string returnHtml = string.Empty; 138 var sortMatchList = from d in highLevelList 139 orderby Convert.ToInt32(d.Value[2]) 140

select d; 141 foreach (var s in sortMatchList) 142 { 143 returnHtml += "<li onclick='getValue(this)'>" + s.Value[1] + "</li>"; 144 matchEntity.Remove(s.Key); 145 listNumber++; 146 } 147 var lowLevelList = matchEntity.OrderByDescending(d => d.Value[0]); 148 int number = 0; 149 foreach (var s in lowLevelList) 150 { 151 if (number < (10 - listNumber)) 152 { 153 returnHtml += "<li onclick='getValue(this)'>" + s.Value[1] + "</li>"; 154 number++; 155 } 156 else 157 { 158 break; 159 } 160 } 161 return returnHtml; 162 } 163 else 164 { 165 int listNumber = 0; 166 string returnHtml = string.Empty; 167 var sortMatchList = from d in highLevelList 168 orderby Convert.ToInt32(d.Value[2]) 169 select d; 170 for (int i = 0; i < sortMatchList.ToList().Count; i++) 171 { 172 if (listNumber < 10) 173 { 174 returnHtml += "<li onclick='getValue(this)'>" + sortMatchList.ToList()[i].V175 alue[1] + "</li>"; 176 listNumber++; 177 } 178 else 179 { 180 break; 181 } 182 } 183 return returnHtml; 184 } 185 186 #endregion 187 } 188 } 189 } 190 191 192 /****************************** Module Header ******************************\ 193 * This source is subject to the Microsoft Public License. 194 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. 195 * All other rights reserved. 196 * 197 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 198 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 199 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. 200 \*****************************************************************************/ 201 202 203 204 using System; 205 using System.Collections; 206 using System.Configuration; 207 using System.Data; 208

using System.Linq; 209 using System.Web; 210 using System.Web.Security; 211 using System.Web.UI; 212 using System.Web.UI.HtmlControls; 213 using System.Web.UI.WebControls; 214 using System.Web.UI.WebControls.WebParts; 215 using System.Text.RegularExpressions; 216 using System.IO; 217 218 namespace CSASPNETIntelligentTextBox 219 { 220 public partial class WordAddPage : System.Web.UI.Page 221 { 222 protected void Page_Load(object sender, EventArgs e) 223 { 224 225 } 226 227 /// <summary> 228 /// Add new words 229 /// </summary> 230 /// <param name="sender"></param> 231 /// <param name="e"></param> 232 protected void btnSubmit_Click(object sender, EventArgs e) 233 { 234 string validateResult = StringValidate(tbNewWords.Text); 235 if (string.IsNullOrEmpty(validateResult)) 236 { 237 string[] words = tbNewWords.Text.Trim().Split(','); 238 using (StreamWriter writer = new StreamWriter(Server.MapPath("~/Dictionar239 y/WordList.txt"), true)) 240 { 241 foreach (string str in words) 242 { 243 writer.WriteLine(str); 244 } 245 lbMessage.Text = "Congratulations, the new word has been added in Wor246 d dictionary."; 247 } 248 } 249 else 250 { 251 lbMessage.Text = validateResult; 252 } 253 } 254 255 /// <summary> 256 /// String variables validation. 257 /// </summary> 258 /// <param name="strWords"></param> 259 /// <returns></returns> 260 protected static string StringValidate(string strWords) 261 { 262 string words = strWords.Trim(); 263 if (string.IsNullOrEmpty(words)) 264 { 265 return "Words can not be null."; 266 } 267 else if (StringIncludeNumberic(words)) 268 { 269 return "Words can not include numeral and special characters."; 270 } 271

else 272 { 273 return string.Empty; 274 } 275 } 276 277 protected static bool StringIncludeNumberic(string strWords) 278 { 279 string strRegex = @"[^a-zA-z,']"; 280 return Regex.IsMatch(strWords, strRegex); 281 } 282 283 protected void btnBack_Click(object sender, EventArgs e) 284 { 285 Response.Redirect("~/Default.aspx"); 286 } 287 } 288 } 289 290