-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhrmlparser.java
More file actions
121 lines (101 loc) · 2.44 KB
/
hrmlparser.java
File metadata and controls
121 lines (101 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.*;
class Tag
{
HashMap<String,String> nvpairs;
HashMap<String,Tag> childTags;
public Tag()
{
nvpairs=new HashMap<String,String>();
childTags=new HashMap<String,Tag>();
}
public void addTag(String tname,Tag t)
{
childTags.put(tname,t);
}
}
class HRMLParser
{
Tag root;
Stack<Tag> tagStack;
public HRMLParser()
{
root=new Tag();
tagStack=new Stack<Tag>();
tagStack.push(root);
}
public void parseLine(String line)
{
//Format --> <tagName attribute1 = "val1" attribute2 = "val2" etc.>
line=line.substring(1,line.length()-1); //Strip < >
if(line.charAt(0)!='/')//If starting tag
{
int firstSpace=line.indexOf(" ");
String tagName;
Tag newTag=new Tag();
if(firstSpace>0)
{
tagName=line.substring(0,firstSpace);
line+=" ";
String[] nvs=line.substring(firstSpace+1,line.length()).split("\" ");
String name,value;
String[] pair;
for(int i=0;i<nvs.length;i++)
{
pair=nvs[i].split("="); // [ <space>attribute_x<space> , <space>"value_x ]
name=pair[0].trim(); // <space>attribute_x<space> --> attribute_x
value=pair[1].trim(); // <space>"value_x --> "value_x
value=value.substring(1); // Remove leading quote "value_x--> value_x
//System.out.println(name+"="+value);
newTag.nvpairs.put(name,value);
}
}
else
tagName=line;
Tag parent=tagStack.peek();
parent.addTag(tagName,newTag);
tagStack.push(newTag);
System.out.println(tagName);
}
else
tagStack.pop();
}
public void eval(String str)
{
String[] tagInfo=str.split("\\.");
Tag currentTag=root;
if(tagInfo.length>0)
{
String[] reqTag=tagInfo[tagInfo.length-1].split("~");
//System.out.println(tagInfo[tagInfo.length-1]);
for(int i=0;i<tagInfo.length-1;i++)
currentTag=currentTag.childTags.get(tagInfo[i]);
System.out.println(currentTag.childTags.get(reqTag[0]).nvpairs.get(reqTag[1]));
}
else
{
String[] reqTag=str.split("~");
System.out.println(reqTag[0]);
System.out.println(currentTag.childTags.get(reqTag[0]).nvpairs.get(reqTag[1]));
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n,q;
n=sc.nextInt();
q=sc.nextInt();
sc.nextLine();
String line;
String query;
HRMLParser parser=new HRMLParser();
for(int i=0;i<n;i++)
{
line=sc.nextLine();
parser.parseLine(line);
}
for(int i=0;i<q;i++)
{ query=sc.nextLine();
parser.eval(query);
}
}
}