// SSDLPediapackageil.ac.technion.cs.ssdl.strings;
importstaticil.ac.technion.cs.ssdl.strings.RE.anyNumberOfReluctant;
importstaticil.ac.technion.cs.ssdl.strings.RE.group;
importstaticil.ac.technion.cs.ssdl.strings.RE.ignoreCase;
importstaticil.ac.technion.cs.ssdl.utils.DBC.nonnull;
importstaticorg.junit.Assert.assertEquals;
importil.ac.technion.cs.ssdl.stereotypes.Immutable;
importil.ac.technion.cs.ssdl.stereotypes.Instantiable;
importil.ac.technion.cs.ssdl.utils.Separate;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importorg.junit.Test;
/** * A representation of an HTML tag, capable of wrapping a given String. * * Author: Yossi Gil, the Technion. * See: 11/07/2008 */@Immutable@InstantiablepublicclassTag {
/** * Wrap a given string within this tag. * * s a non-null representing the string to wrap * Return: the string s wrapped with the tag, e.g., if * s is "Hello" and the tag name is * "b" then "Hello"" is returned. */publicStringwrap(finalStrings) {
nonnull(s);
returns.length() == 0 ? s : begin + s + end;
}
/** * Wrap a given string within newline characters and then within this tag. * * s a non-null representing the string to wrap * Return: the string s wrapped with the tag, e.g., if * s is "Hello" and the tag name is * "b" then the string "\nHello\n" * is returned. */publicStringwrapNL(finalStrings) {
nonnull(s);
returnwrap("\n" + s + "\n");
}
/** * Instantiate a plain tag, i.e., a tag without any inner tags, * * name the tag name, e.g., "font" * flags any number of HTML flags */publicTag(finalStringname, finalString... flags) {
nonnull(name);
nonnull(flags);
begin = beginTag(name + (flags.length == 0 ? "" : " " + Separate.by(flags, " ")));
end = beginTag("/" + name);
}
/** * Instantiate a tag containing another tag * * inner the inner tag; all instances of the newly created tag will * be around this inner tag * name the tag name, e.g., "font" * flags any number of HTML flags */publicTag(finalTaginner, finalStringname, finalString... flags) {
nonnull(name);
nonnull(flags);
finalTagunnested = newTag(name, flags);
begin = unnested.begin + inner.begin;
end = inner.end + unnested.end;
}
/** * A factory function, creating a new tag, containing this one. Typical use * demonstrates tag containment. The expression new Tag("strong").inside("tt") * returns a newly created nested Tag composed of a strong * within a tt tag. * * name the name of the newly created tag, e.g., "font" * flags any number of HTML flags to be used with the newly created * tag * Return: A newly created tag with the specified name and flags, containing * this tag */publicTaginside(finalStringname, finalString... flags) {
returnnewTag(this, name, flags);
}
/** * Make a regular expression to capture the opening and closing tag together * with the enclosed content. * * Return: a regular expression to capture the tag and its content. The * content is in group number 1. */publicStringmakeRegularExpression() {
returnignoreCase() + begin + group(anyNumberOfReluctant(".|[\r\n]")) + end;
}
/** * Make a Pattern to capture the opening and closing tag together * with the enclosed content. * * Return: a regular expression to capture the tag and its content. The * content is in group number 1. */publicPatternmakePattern() {
returnPattern.compile(makeRegularExpression());
}
/** * Make a Matcher of a given text, to capture the opening and * closing tag together with the enclosed content in this text. * * s where to look for this text? * Return: Matcher of the parameter to capture the tag and its * content. The content is in group number 1. */publicMatchermakeMatcher(finalStrings) {
returnmakePattern().matcher(s);
}
/** * The opening String of this tag. */publicfinalStringbegin;
/** * The closing String of this tag. */publicfinalStringend;
/** * A pre-made instance, representing the HTML <strong> tag. */publicfinalstaticTagstrong = newTag("strong");
/** * A pre-made instance, representing the HTML <em> tag. */publicfinalstaticTagem = newTag("em");
/** * A pre-made instance, representing the HTML <pre> tag. */publicfinalstaticTagpre = newTag("pre");
/** * A pre-made instance, representing the HTML <tt> tag. */publicfinalstaticTagtt = newTag("tt");
/** * A pre-made instance, representing the HTML <p> tag, used for * marking a paragraph. */publicfinalstaticTagp = newTag("p");
/** * A pre-made instance, representing the HTML <u> tag, used for * underlining the wrapped text. */publicfinalstaticTagu = newTag("u");
/** * A pre-made instance, representing the HTML <h1> tag. */publicfinalstaticTagh1 = newTag("h1");
/** * A pre-made instance, representing the HTML <h2> tag. */publicfinalstaticTagh2 = newTag("h2");
/** * A pre-made instance, representing the HTML <h3> tag. */publicfinalstaticTagh3 = newTag("h3");
/** * A pre-made instance, representing the HTML <h4> tag. */publicfinalstaticTagh4 = newTag("h4");
/** * A pre-made instance, representing the HTML <h5> tag. */publicfinalstaticTagh5 = newTag("h5");
/** * A pre-made instance, representing the HTML <h6> tag. */publicfinalstaticTagh6 = newTag("h6");
publicstaticStringreplace(finalStringtext, finalStringfrom, finalStringto) {
returntext//
.replaceAll(ignoreCase() + beginTag(from), beginTag(to)) //
.replaceAll(ignoreCase() + endTag(from), endTag(to));
}
publicstaticStringremove(finalStringtext, finalStringtag) {
returntext//
.replaceAll(ignoreCase() + beginTag(tag), "") //
.replaceAll(ignoreCase() + endTag(tag), "") //
.replaceAll(ignoreCase() + selfClosing(tag), "") //
;
}
/** * Make a String of an HTML opening tag with a given name. * * name the name of the given tag. * Return: the name enclosed in angular brackets. */publicstaticStringbeginTag(finalStringname) {
return"<" + name + ">";
}
/** * Make a self closing String of an HTML tag with a given name. * * name the name of the given tag. * Return: the name parameter, followed by slash (/) and enclosed in angular * brackets. */publicstaticStringselfClosing(finalStringname) {
returnbeginTag(name + " /");
}
/** * Make a String of an HTML closing tag with a given name. * * name the name of the given tag. * Return: the name enclosed in angular brackets. */publicstaticStringendTag(finalStringname) {
returnbeginTag("/" + name);
}
publicstaticclassTestMakePattern {
privatestaticfinalStringtagRegularExpression = newTag("dummy").makeRegularExpression();
@TestpublicvoidtestSimplePre() {
assertEquals("ABC", "A<dummy>X</dummy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestEmptyPre() {
assertEquals("ABC", "A<dummy></dummy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestCRLFinPre() {
assertEquals("ABC", "A<dummy>\r\n</dummy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestLFinPre() {
assertEquals("ABC", "A<dummy>\n</dummy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestSeveralLinesInPre() {
assertEquals("ABC", "A<dummy>a\nb\r\nABCDE</dummy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestUpperCaseTag() {
assertEquals("ABC", "A<DUMMY>a\nb\r\nABCDE</DUMMY>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestMiXeDCaSeTag() {
assertEquals("ABC", "A<DuMmY>a\nb\r\nABCDE</dUmMy>C".replaceFirst(tagRegularExpression, "B"));
}
@TestpublicvoidtestDummyInContext() {
assertEquals("" + //"\t /**\r\n" + //"\t * BEFORE\r\n" + //"\t * Content\r\n" + //"\t * AFTER\r\n" + //"\t */" + //"", ("" + //"\t /**\r\n" + // "\t * BEFORE\r\n" + //"\t * <dummy>\r\n" + //"\t * text\r\n" + //"\t * </dummy>\r\n" + //"\t * AFTER\r\n" + //"\t */" + //"").replaceFirst(tagRegularExpression, "Content"));
}
}
}
Metrics
Metric
Value
Acronym
Explanation
LOC
290
Lines Of Code
Total number of lines in the code
SCC
57
SemiColons Count
Total number of semicolon tokens found in the code.
NOT
997
Number Of Tokens
Comments, whitespace and text which cannot be made into a token not included.
VCC
7118
Visible Characters Count
The total number of non-white (i.e., not space, tab, newline, carriage return, form feed) characters.
CCC
3713
Code Characters Count
Total number of non-white characters in tokens. White space characters in string and character literals are not counted.
UIC
79
Unique Identifiers Count
The number of different identifiers found in the code