*** Here is the link to download required documents. https://drive.google.com/op
ID: 3860360 • Letter: #
Question
*** Here is the link to download required documents.
https://drive.google.com/open?id=0B2gb5h869g2aTFpkY0hCUkVfLUk
Create a program that validates the nesting of elements in an XML document. The nesting rule simply states that while elements may be nested, they cannot overlap. This is one of the rules that must be satisfied for an XML document to be considered well-formed. For more information on XML documents, see this page and this page.
Tasks and Requirements
NOTE: Naming is critical in the tasks and requirements described below. If the names don't match those described below exactly, your project will not compile and can't be graded.
Create a copy of the Java SE Project Template.
The project name must follow this pattern: {FLname}_XmlValidator_{TERM}, where {FLname} is replaced by the first letter of your first name plus your last name, and {TERM} is the current semester and year. E.g. if your name is Maria Marciano and its Fall 2025, your project name must be MMarciano_XmlValidator_F25. If your project name does not follow this pattern, it will not be graded.
In your src folder, create a package named xmlvalidator.
BasicStringStack class:
Download the StringStack.java interface and put it in the xmlvalidator package. Read the comments above each method to understand what they are supposed to do.
Create a new class named BasicStringStack in the xmlvalidator package. In the class creation wizard, click the Add (interface) button, and search for StringStack. Check the "Inherited abstract methods" box. Click Finish. Eclipse will create the class and automatically add method stubs that meet the StringStackinterface.
You do not modify the StringStack interface. You add your code in the BasicStringStack class.
You must write your own stack code - i.e. you can't use java.util.Stack or any other existing implementation. You are welcome to use the guidebook's code as a guide if you like.
BasicXmlValidator class:
Download the XmlValidator.java interface and put it in the xmlvalidator package. Read the comments above each method to understand what they are supposed to do.
Create a new class named BasicXmlValidator in the xmlvalidator package. In the class creation wizard, click the Add (interface) button, and search for XmlValidator. Check the "Inherited abstract methods" box. Click Finish. Eclipse will create the class and automatically add method stubs that meet the XmlValidator interface.
You do not modify the XmlValidator interface. You add your code in the BasicXmlValidator class.
Validation notes:
Ignore any tag that doesn't start with '/' or a letter character. This includes xml version/encoding elements like <?xml version="1.0" encoding="UTF-8"?>, and comments like <!-- diblah -->.
Ignore self-closing tags like <sometag />.
Ignore comments like <!-- This is a comment -->.
Line numbers start at 1. ' ' marks the end of a line.
You can assume that the XML document won't have any CDATA sections.
Note that XML start tags can span multiple lines. E.g. :
<sometag
attr1="blah"
attr2="diblah">
Add the unit testing classes. These classes will be used to test the code that you write.
Create a new package in the src folder called sbccunittest. To be clear: sbccunittest should be a child of the src folder, not of the xmlvalidator package.
Download XmlValidatorTester.java into sbccunittest.
Here is a sample XML file with invalid nesting (right-click "Save Link As..."). Calling validate() with the contents of the file should return the following String[]: { "Tag mismatch", "name", "14", "buildCommand", "17" }
Unit Testing
Debug all java compilation Errors (see the Problems view). The unit tests can't be run until these errors are fixed. NOTE: as shown above, some of the XML test files will have errors. Don't try to fix these files, the errors are meant to be detected by your BasicXmlValidator class.
In the Package Explorer, right-click on the sbccunittest package | Run As... | JUnit Test.
Initially, all of the tests will probably fail. However, as you add functionality to the BasicStringStack class, tests will begin to pass.
Work on BasicStringStack first. Continue adding functionality to the BasicStringStack until all of the tests in BasicStringStackTester pass.
*** Here is the link to download required documents.
https://drive.google.com/open?id=0B2gb5h869g2aTFpkY0hCUkVfLUk
There is no user interface requirement (i.e. no Main class) for this assignment.
Explanation / Answer
Main.java
package xmlvalidator;
import static java.lang.System.*;
import java.util.*;
//Used for testing various functions with basicstringStack and XMLValidator
public class Main {
public static void main(String[] args) {
String openTag = "(<(?<openTag>\w[\w\d]*)(?<openOther>[^>]*)>)";
String closeTag = "(</(?<closeTag>\w[\w\d]*)>)";
String newLine = "(?<newLine> )";
// Original - save for reference.
// "(<(?<openTag>\w[\w\d]*)(?<openOther>[^>]*)>)|(</(?<closeTag>\w[\w\d]*)>)|(?<newLine> )");
ArrayList<String> R = new ArrayList<String>();
R.add(openTag);
R.add(closeTag);
R.add(newLine);
StringBuilder allRegex = new StringBuilder("");
for (String regex : R) {
allRegex.append(regex);
allRegex.append("|");
}
allRegex.setLength(allRegex.length() - 1);
String regex = allRegex.toString();
out.println(regex);
}
}
BasicXmlValidator.java
package xmlvalidator;
import static org.apache.commons.lang3.StringUtils.*;
import java.util.*;
import java.util.regex.*;
public class BasicXmlValidator implements XmlValidator {
@Override
public String[] validate(String xmlDocument) {
// Two stacks - one keeps track of last open tag, other the line number it was on
// TODO wrap these in a function so they're always called together.
BasicStringStack S = new BasicStringStack();
BasicStringStack L = new BasicStringStack();
// build matcher with complicated regex - explained below with buildValidatorRegex()
Pattern P = Pattern.compile(buildValidatorRegex());
Matcher m = P.matcher(xmlDocument);
int linecount = 1;
String[] error = new String[5];
// loop through string
while (m.find()) {
// Found Open Tag - add to stack
if (m.group("openTag") != null) {
S.push(m.group("openTag"));
L.push(Integer.toString(linecount));
}
// TODO: count newlines in openOther to get starting line number
// Currently line number is where the tag closes
// Found closing tag - pop from stack and make sure they match
// Error if stack is empty (orphan) or in case of mismatch.
if (m.group("closeTag") != null) {
String closeTag = m.group("closeTag");
String expectedTag = S.pop();
if (expectedTag == null) {
error[0] = "Orphan closing tag";
error[1] = closeTag;
error[2] = Integer.toString(linecount);
return error;
}
String expectedLineNum = L.pop();
if (!expectedTag.equals(closeTag)) {
error[0] = "Tag mismatch";
error[1] = expectedTag;
error[2] = expectedLineNum;
error[3] = closeTag;
error[4] = Integer.toString(linecount);
return error;
}
}
// Found newline - update line counter
if (m.group("newLine") != null) {
linecount++;
}
// Found Attribute - check to see if it has quotes
if (m.group("openOther") != null) {
String attribute = checkAttribQuotes(m.group("openOther"));
if (attribute != null) {
error[0] = "Attribute not quoted";
error[1] = m.group("openTag");
error[2] = Integer.toString(linecount);
error[3] = attribute;
error[4] = Integer.toString(linecount);
return error;
}
}
} // elihw - endWhile
// Check stack - should be empty at this point
// If not error on unclosed tag
String stackCheck = S.pop();
if (stackCheck != null) {
error[0] = "Unclosed tag at end";
error[1] = stackCheck;
error[2] = Integer.toString(linecount);
return error;
}
return null;
}// end validate
// Very basic check on attributes
// if there aren't 2 " for every = return attribute as error
// TODO: make this actually check position of quotes
// TODO: Fix regex to handle invalid attributes
private String checkAttribQuotes(String attrib) {
int equals = countMatches(attrib, "=");
int quotes = countMatches(attrib, """);
if (2 * equals != quotes) {
Pattern P = Pattern.compile("((?<attrib>[\w\d]+)=)");
Matcher m = P.matcher(attrib);
m.find();
return m.group("attrib");
}
return null;
}
private String buildValidatorRegex() {
String openTag = "(<(?<openTag>\w[\w\d]*)(?<openOther>[^>]*)>)";
String closeTag = "(</(?<closeTag>\w[\w\d]*)>)";
String newLine = "(?<newLine> )";
// Original - save for reference.
// really could just return this and all would still be good
// "(<(?<openTag>\w[\w\d]*)(?<openOther>[^>]*)>)|(</(?<closeTag>\w[\w\d]*)>)|(?<newLine> )");
ArrayList<String> R = new ArrayList<String>();
R.add(openTag);
R.add(closeTag);
R.add(newLine);
StringBuilder allRegex = new StringBuilder("");
for (String regex : R) {
allRegex.append(regex);
allRegex.append("|");
}
allRegex.setLength(allRegex.length() - 1); // remove last | character from builder
String regex = allRegex.toString();
return regex;
// }
}
}
BasicStringStack.java
package xmlvalidator;
public class BasicStringStack implements StringStack {
private String[] stackArray;
// private static Logger l;
private int top;
public BasicStringStack() {
super();
stackArray = new String[2];
stackArray[0] = null;
top = 0; // array will be 1 indexed - so top can be used as count
// l = Logger.getLogger("BasicStringStack");
// l.info("Created BasicStringStack");
}
@Override
public void push(String item) {
if (top == stackArray.length - 1) { // if Array fills up - copied to a double sized one.
String[] temp = stackArray;
stackArray = new String[stackArray.length * 2];
System.arraycopy(temp, 0, stackArray, 0, temp.length);
// l.info("Increased stackArray size to " + stackArray.length);
}
top += 1;
stackArray[top] = item;
// l.info("Pushed String{" + item + "} on to stack. Size is:" + top);
}
@Override
public String pop() {
if (top == 0)
return null;
// if(top < stackArray / 3){} //TODO if array is 1/3rd full - resize to half
String retval = stackArray[top];
top--;
return retval;
}
@Override
public String peek(int position) {
if (position >= top || position < 0) // return null for values out of range of stack
return null;
int index = top - position;
return stackArray[index];
}
@Override
public int getCount() {
return top;
}
}
StringStack.java
package xmlvalidator;
public interface StringStack {
/**
* Pushes the given item onto the top of the stack.
*
* @param item
*/
public void push(String item);
/**
* Removes the top item from the stack.
*
* @return The removed item.
*/
public String pop();
/**
* Returns, but does not remove, the item at the given position. 0 is the top, 1 is the second item, and so on.
*
* @param position
*
* @return The item at the given position.
*/
public String peek(int position);
/**
* @return The number of items on the stack
*/
public int getCount();
}
XmlValidator.java
package xmlvalidator;
public interface XmlValidator {
public String[] validate(String xmlDocument);
}
XmlValidatorTester.java
package sbccunittest;
import static org.junit.Assert.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import org.apache.commons.io.*;
import org.junit.*;
import xmlvalidator.*;
public class XmlValidatorTester {
//The following tests will be done on a locally created String rather than the contents of a downloaded file
//they have proprietary names but are not case-sensitive
public ArrayList<String> localStrings = new ArrayList<String>(Arrays.asList("Valid File", "Big Valid File", "Unclosed Tag at End", "Orphan Closing Tag", "Attribute Not Quoted", "Unclosed Tag"));
//The following tests will be done on a downloaded file, not a locally created String
public ArrayList<String> fileStrings = new ArrayList<String>();
BasicXmlValidator validator;
BasicStringStack stack;
HashMap<String, String> testStrings;
Random randomGenerator = new Random();
ArrayList<String> possibleAttributes = new ArrayList<String>(Arrays.asList("Version", "default", "pattern", "value", "color", "property", "name", "outfile"));
ArrayList<String> possibleValues = new ArrayList<String>(Arrays.asList("1.0", "dark", "dd/mm/yyyy", "503", "#FFFFFF", "primaryID", "tagName", "file.dat"));
String standardXMLDeclarationTag = "<?xml version="1.0" encoding="UTF-8"?>";
int nestLevel = 0; //used to track proper indentation
int overallNesting = 0; //used to give the tag labels a root-> child
int lineNumber = 0;
HashMap<String, String> errorTags = new HashMap<String, String>();
HashMap<String, String> errorParameters = new HashMap<String, String>();
HashMap<String, String> errorLines = new HashMap<String, String>();
String unclosedEndTagParentLine;
public static int totalScore = 0;
public static int extraCredit = 0;
@BeforeClass
public static void beforeTesting() {
totalScore = 0;
extraCredit = 0;
}
@AfterClass
public static void afterTesting() {
System.out.println("Estimated score (assuming no late penalties, etc.) = " + totalScore);
System.out.println("Estimated extra credit (assuming on time submission) = " + extraCredit);
}
@Before
public void setUp() throws Exception {
stack = new BasicStringStack();
validator = new BasicXmlValidator();
//Load or Construct Strings for testing
prepareTestingStrings();
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPush() {
int numberOfStringsToTest = randomGenerator.nextInt(3) + 1;
String[] strings = new String[numberOfStringsToTest];
String tString;
for(int i = 1; i <= numberOfStringsToTest; i++){
tString = getRandomString();
strings[i - 1] = tString;
stack.push(tString);
}
for(int i = 1; i <= numberOfStringsToTest; i++){
assertEquals(strings[i - 1], stack.peek(numberOfStringsToTest - i));
}
totalScore += 2;
}
@Test
public void testPop() {
int i;
int numberOfStringsToTest = randomGenerator.nextInt(3) + 1;
String[] strings = new String[numberOfStringsToTest];
String tString;
for(i = 1; i <= numberOfStringsToTest; i++){
tString = getRandomString();
strings[i - 1] = tString;
stack.push(tString);
}
for(i = numberOfStringsToTest; i > 1; i--){
assertEquals(strings[i - 1], stack.pop());
}
stack.pop();
assertEquals(null, stack.pop());
totalScore += 3;
}
@Test
public void testExercise() {
int i;
int numberOfStringsToTest = randomGenerator.nextInt(3) + 3;
String[] strings = new String[numberOfStringsToTest];
String tString;
for(i = 1; i <= numberOfStringsToTest; i++){
tString = getRandomString();
strings[i - 1] = tString;
stack.push(tString);
}
assertEquals(stack.peek(0), strings[numberOfStringsToTest - 1]);
assertEquals(numberOfStringsToTest, stack.getCount());
assertEquals(strings[numberOfStringsToTest - 3], stack.peek(2));
assertEquals(stack.pop(), strings[numberOfStringsToTest - 1]);
assertEquals(numberOfStringsToTest - 1, stack.getCount());
for(i = 1; i < numberOfStringsToTest; i++){
assertEquals(strings[numberOfStringsToTest - i - 1], stack.pop());
}
assertEquals(0, stack.getCount());
stack.pop();
stack.pop();
assertEquals(null, stack.pop());
totalScore += 5;
}
@Test
public void testValidFile() {
String xmlDocument = testStrings.get("valid file");
String[] result = validator.validate(xmlDocument);
assertNull(result);
totalScore += 10;
}
@Test
public void testBigValidFile() throws IOException {
String xmlDocument = testStrings.get("big valid file");
String[] result = validator.validate(xmlDocument);
assertNull(result);
totalScore += 5;
}
@Test
public void testOrphanClosingTag() throws IOException {
String xmlDocument = testStrings.get("orphan closing tag");
String[] result = validator.validate(xmlDocument);
assertEquals("Orphan closing tag", result[0]);
assertEquals(errorTags.get("orphan closing tag"), result[1]);
assertEquals(errorLines.get("orphan closing tag"), result[2]);
totalScore += 5;
}
@Test
public void testUnclosedTag() throws IOException {
String xmlDocument = testStrings.get("unclosed tag");
String[] result = validator.validate(xmlDocument);
assertEquals("Tag mismatch", result[0]);
assertEquals(errorTags.get("unclosed tag"), result[1]);
assertEquals(errorLines.get("unclosed tag"), result[2]);
assertEquals(errorParameters.get("unclosed tag"), result[3]);
assertEquals(unclosedEndTagParentLine, result[4]);
totalScore += 10;
}
@Test
public void testUnclosedTagAtEnd() throws IOException {
String xmlDocument = testStrings.get("unclosed tag at end");
String[] result = validator.validate(xmlDocument);
assertEquals("Unclosed tag at end", result[0]);
assertEquals(errorTags.get("unclosed tag at end"), result[1]);
assertEquals(errorLines.get("unclosed tag at end"), result[2]);
totalScore += 10;
}
@Test
public void testAttributeNotQuoted() throws IOException {
String xmlDocument = testStrings.get("attribute not quoted");
String[] result = validator.validate(xmlDocument);
assertEquals("Attribute not quoted", result[0]);
assertEquals(errorTags.get("attribute not quoted"), result[1]);
assertEquals(errorLines.get("attribute not quoted"), result[2]);
assertEquals(errorParameters.get("attribute not quoted"), result[3]);
assertEquals(errorLines.get("attribute not quoted"), result[4]);
extraCredit += 3;
}
public void prepareTestingStrings() throws IOException{
testStrings = new HashMap<String, String>();
for(String l : localStrings){
testStrings.put(l.toLowerCase(), getLocalString(l));
}
for(String l : fileStrings){
testStrings.put(l.toLowerCase(), getFileString(l));
}
}
public String getFileString(String testName) throws IOException{
if(testName.equalsIgnoreCase("unclosed tag")){
return FileUtils.readFileToString(new File("TestFile1.xml"));
}
if(testName.equalsIgnoreCase("unclosed tag at end")){
return FileUtils.readFileToString(new File("TestFile2.xml"));
}
if(testName.equalsIgnoreCase("valid file")){
return FileUtils.readFileToString(new File("TestFile3.xml"));
}
if(testName.equalsIgnoreCase("big valid file")){
return FileUtils.readFileToString(new File("TestFile4.xml"));
}
if(testName.equalsIgnoreCase("attribute not quoted")){
return FileUtils.readFileToString(new File("TestFile5.xml"));
}
if(testName.equalsIgnoreCase("orphan closing tag")){
return FileUtils.readFileToString(new File("TestFile6.xml"));
}
return "";
}
public String getLocalString(String testName){
if(testName.equalsIgnoreCase("unclosed tag")){
return constructXMLWithUnclosedTag(2, 2, true);
}
if(testName.equalsIgnoreCase("unclosed tag at end")){
return constructXMLWithUnclosedEndTag(2, 2, true);
}
if(testName.equalsIgnoreCase("valid file")){
return constructValidXMLString(2, 1, true);
}
if(testName.equalsIgnoreCase("big valid file")){
return constructValidXMLString(5, 2, true, true);
}
if(testName.equalsIgnoreCase("attribute not quoted")){
return constructXMLWithUnquotedAttribute(5, 2, true);
}
if(testName.equalsIgnoreCase("orphan closing tag")){
return constructXMLWithOrphanClosingTag(4, 2, true);
}
return "";
}
public String constructValidXMLString(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes){
return constructValidXMLString(minimumNestingLevel, minimumNumberOfTags, includeAttributes, false);
}
public String constructValidXMLString(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes, boolean includeComments){
StringBuilder sb = new StringBuilder();
sb.append(standardXMLDeclarationTag);
lineNumber = 2;
overallNesting = minimumNestingLevel;
sb.append(" <rootTag>");
while(minimumNumberOfTags > 0){
sb.append(getXMLTag(minimumNestingLevel - 1, includeAttributes, includeComments));
minimumNumberOfTags--;
}
sb.append(" </rootTag>"); lineNumber++;
return sb.toString();
}
public String constructXMLWithUnclosedTag(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes){
return constructXMLWithUnclosedTag(minimumNestingLevel, minimumNumberOfTags, includeAttributes, false);
}
public String constructXMLWithUnclosedTag(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes, boolean includeComments){
StringBuilder sb = new StringBuilder();
sb.append(standardXMLDeclarationTag);
lineNumber = 2;
overallNesting = minimumNestingLevel;
sb.append(" <rootTag>");
while(minimumNumberOfTags > 1){
sb.append(getXMLTag(minimumNestingLevel - 1, includeAttributes, includeComments));
minimumNumberOfTags--;
}
sb.append(getUnclosedXMLTagTest("unclosed tag"));
sb.append(" </rootTag>"); lineNumber++;
return sb.toString();
}
public String constructXMLWithUnclosedEndTag(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes){
return constructXMLWithUnclosedEndTag(minimumNestingLevel, minimumNumberOfTags, includeAttributes, false);
}
public String constructXMLWithUnclosedEndTag(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes, boolean includeComments){
StringBuilder sb = new StringBuilder();
sb.append(standardXMLDeclarationTag);
lineNumber = 2;
overallNesting = minimumNestingLevel;
sb.append(" <rootTag>");
while(minimumNumberOfTags > 1){
sb.append(getXMLTag(minimumNestingLevel - 1, includeAttributes, includeComments));
minimumNumberOfTags--;
}
sb.append(" <unclosedEnd>Content"); lineNumber++;
errorTags.put("unclosed tag at end", "unclosedEnd");
errorLines.put("unclosed tag at end", Integer.toString(lineNumber));
return sb.toString();
}
public String constructXMLWithUnquotedAttribute(int minimumNestingLevel, int minimumNumberOfTags, boolean includeComments){
StringBuilder sb = new StringBuilder();
sb.append(standardXMLDeclarationTag);
lineNumber = 2;
overallNesting = minimumNestingLevel;
sb.append(" <rootTag>");
int unquotedAttributesTag = randomGenerator.nextInt(minimumNumberOfTags - 1) + 1;
while(minimumNumberOfTags > 0){
sb.append(getXMLTag(minimumNestingLevel - 1, true, includeComments, minimumNumberOfTags != unquotedAttributesTag));
minimumNumberOfTags--;
}
sb.append(" </rootTag>"); lineNumber++;
return sb.toString();
}
public String constructXMLWithOrphanClosingTag(int minimumNestingLevel, int minimumNumberOfTags, boolean includeAttributes){
StringBuilder sb = new StringBuilder();
sb.append(standardXMLDeclarationTag);
lineNumber = 2;
overallNesting = minimumNestingLevel;
sb.append(" <rootTag>");
while(minimumNumberOfTags > 1){
sb.append(getXMLTag(minimumNestingLevel - 1, includeAttributes, false));
minimumNumberOfTags--;
}
sb.append(" </rootTag>"); lineNumber++;
errorTags.put("orphan closing tag", "level" + Integer.toString(minimumNestingLevel - 1));
String extraCloser = " </" + errorTags.get("orphan closing tag") + ">"; lineNumber++;
sb.append(extraCloser);
errorLines.put("orphan closing tag", Integer.toString(lineNumber));
return sb.toString();
}
public String getXMLTag(int childNestingLevel, boolean includeAttributes){
return getXMLTag(childNestingLevel, includeAttributes, false);
}
public String getXMLTag(int childNestingLevel, boolean includeAttributes, boolean includeComments){
return getXMLTag(childNestingLevel, includeAttributes, includeComments, true);
}
public String getXMLTag(int childNestingLevel, boolean includeAttributes, boolean includeComments, boolean quoteAttributes){
StringBuilder sb = new StringBuilder();
String tagName = "level" + Integer.toString(overallNesting - childNestingLevel);
sb.append(" "); lineNumber++;
for(int i = 0; i <= nestLevel; i++){
sb.append(" ");
}
if(includeComments){
sb.append(" ");
for(int i = 0; i <= nestLevel; i++){
sb.append(" ");
}
sb.append("<!-- This is a comment --> "); lineNumber += 2;
for(int i = 0; i <= nestLevel; i++){
sb.append(" ");
}
}
sb.append("<").append(tagName).append(" ");
if(includeAttributes){
sb.append(getRandomAttribute(quoteAttributes));
if(!quoteAttributes){
errorTags.put("attribute not quoted", tagName);
errorLines.put("attribute not quoted", Integer.toString(lineNumber));
quoteAttributes = true;
}
}
sb.append(">");
if(childNestingLevel > 0){
nestLevel++;
int children = randomGenerator.nextInt(3) + 1;
for(int c = 0; c < children; c++){
sb.append(getXMLTag(childNestingLevel - 1, includeAttributes));
}
nestLevel--;
sb.append(" "); lineNumber++;
for(int i = 0; i <= nestLevel; i++){
sb.append(" ");
}
}
else{
sb.append("Tag Content");
}
sb.append("</").append(tagName).append(">");
return sb.toString();
}
public String getUnclosedXMLTagTest(String test){
StringBuilder sb = new StringBuilder();
errorLines.put(test, Integer.toString((lineNumber += 2)));
errorTags.put(test, "unclosed");
errorParameters.put(test, "parentTag");
unclosedEndTagParentLine = Integer.toString(lineNumber += 2);
sb.append(" <parentTag>").append(" <unclosed>Content ").append(" </parentTag>"); lineNumber += 2;
return sb.toString();
}
public String getRandomAttribute(){
return getRandomAttribute(true);
}
public String getRandomAttribute(boolean quoted){
StringBuilder sb = new StringBuilder();
int index = randomGenerator.nextInt(possibleAttributes.size());
String attributeName = possibleAttributes.get(index);
index = randomGenerator.nextInt(possibleValues.size());
String value = possibleValues.get(index);
sb.append(attributeName).append("=");
if(quoted){
sb.append(""");
}
sb.append(value);
if(quoted){
sb.append(""");
}
else{
errorParameters.put("attribute not quoted", attributeName);
}
return sb.toString();
}
public String getRandomString(){
return getRandomString(4);
}
public String getRandomString(int stringLength){
return getRandomString(stringLength, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
public String getRandomString(int stringLength, String possibleCharacters){
return getRandomString(stringLength, possibleCharacters, possibleCharacters);
}
public String getRandomString(int stringLength, String possibleCharacters, String startingCharacters){
StringBuilder sb = new StringBuilder(stringLength);
sb.append( startingCharacters.charAt(randomGenerator.nextInt( startingCharacters.length() )) );
for( int i = 1; i < stringLength; i++ ){
sb.append( possibleCharacters.charAt(randomGenerator.nextInt( possibleCharacters.length() )) );
}
return sb.toString();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.