CssImporterResource.java
6.24 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.siteimporter.internal.resource;
import com.day.cq.wcm.siteimporter.ImporterContext;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResource;
import com.day.cq.wcm.siteimporter.internal.resource.ImporterResourceVisitor;
import com.day.cq.wcm.siteimporter.internal.resource.ReferenceLocation;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CssImporterResource
extends ImporterResource {
private final Logger log;
public CssImporterResource(URL location, ImporterContext ctx) {
super(location, ctx);
this.log = LoggerFactory.getLogger(this.getClass());
}
@Override
public Object triggerAction(ImporterResourceVisitor is) {
return is.performCss(this);
}
@Override
public ArrayList<ReferenceLocation> getExternalReferences() {
ArrayList<ReferenceLocation> result = new ArrayList<ReferenceLocation>();
String content = "";
try {
content = this.getContent();
}
catch (IOException e) {
return result;
}
int next = content.toLowerCase().indexOf("@import");
while (next > -1) {
next += 7;
while (next < content.length() && Character.isWhitespace(content.charAt(next))) {
++next;
}
switch (content.charAt(next)) {
int end;
case '\'': {
end = content.indexOf("'", next + 1);
if (end <= next) break;
try {
result.add(new ReferenceLocation(next + 1, end, this.toAbsoluteReference(content.substring(next + 1, end)), 1));
}
catch (MalformedURLException e) {}
break;
}
case '\"': {
end = content.indexOf("\"", next + 1);
if (end <= next) break;
try {
result.add(new ReferenceLocation(next + 1, end, this.toAbsoluteReference(content.substring(next + 1, end)), 1));
break;
}
catch (MalformedURLException e) {
break;
}
}
}
next = content.toLowerCase().indexOf("@import", next);
}
next = content.toLowerCase().indexOf("url");
try {
while (next > -1) {
int start;
int end = start = next;
int pos = start;
String link = "";
int state = 0;
int type = 3;
if (content.toLowerCase().lastIndexOf("@import", next) > content.lastIndexOf("{", next)) {
type = 1;
}
while (state < 5) {
char nextChar = content.charAt(pos);
switch (state) {
case 0: {
if (nextChar != '(') break;
state = 1;
break;
}
case 1: {
if (nextChar == '\"') {
state = 3;
start = pos + 1;
break;
}
if (nextChar == '\'') {
state = 4;
start = pos + 1;
break;
}
if (nextChar == ')') {
state = 6;
break;
}
if (Character.isWhitespace(nextChar)) break;
link = link + nextChar;
state = 2;
start = pos;
break;
}
case 2: {
if (Character.isWhitespace(nextChar) || nextChar == ')') {
end = pos;
state = 5;
break;
}
link = link + nextChar;
break;
}
case 3: {
if (nextChar == '\"') {
end = pos;
state = 5;
break;
}
link = link + nextChar;
break;
}
case 4: {
if (nextChar == '\'') {
end = pos;
state = 5;
break;
}
link = link + nextChar;
}
}
if (++pos <= content.length()) continue;
state = 6;
}
if (state == 5) {
try {
result.add(new ReferenceLocation(start, end, this.toAbsoluteReference(link), type));
}
catch (MalformedURLException e) {
// empty catch block
}
}
next = content.toLowerCase().indexOf("url", next + 3);
}
}
catch (StringIndexOutOfBoundsException e) {
this.log.debug("Found unsupported pattern in external reference: {}", (Object)content, (Object)e);
}
catch (Exception e) {
this.log.error("Error while extracting external reference from: {}", (Object)content, (Object)e);
}
return result;
}
public void setContent(String content) {
this.content = content;
this.references = new ArrayList();
}
}