| 1 |
/* |
| 2 |
* Copyright (C) 2011 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.ide.common.resources; |
| 18 |
|
| 19 |
import com.android.SdkConstants; |
| 20 |
import com.android.annotations.NonNull; |
| 21 |
import com.google.common.io.Closeables; |
| 22 |
|
| 23 |
import org.kxml2.io.KXmlParser; |
| 24 |
import org.xmlpull.v1.XmlPullParser; |
| 25 |
import org.xmlpull.v1.XmlPullParserException; |
| 26 |
|
| 27 |
import java.io.BufferedInputStream; |
| 28 |
import java.io.FileInputStream; |
| 29 |
import java.io.IOException; |
| 30 |
import java.io.InputStream; |
| 31 |
|
| 32 |
/** |
| 33 |
* Parser for scanning an XML resource file and validating all framework |
| 34 |
* attribute references in it. If an error is found, the associated context |
| 35 |
* is marked as needing a full AAPT run. |
| 36 |
*/ |
| 37 |
public class ValidatingResourceParser { |
| 38 |
private final boolean mIsFramework; |
| 39 |
private ScanningContext mContext; |
| 40 |
|
| 41 |
/** |
| 42 |
* Creates a new {@link ValidatingResourceParser} |
| 43 |
* |
| 44 |
* @param context a context object with state for the current update, such |
| 45 |
* as a place to stash errors encountered |
| 46 |
* @param isFramework true if scanning a framework resource |
| 47 |
*/ |
| 48 |
public ValidatingResourceParser( |
| 49 |
@NonNull ScanningContext context, |
| 50 |
boolean isFramework) { |
| 51 |
mContext = context; |
| 52 |
mIsFramework = isFramework; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Parse the given input and return false if it contains errors, <b>or</b> if |
| 57 |
* the context is already tagged as needing a full aapt run. |
| 58 |
* |
| 59 |
* @param path the full OS path to the file being parsed |
| 60 |
* @param input the input stream of the XML to be parsed (will be closed by this method) |
| 61 |
* @return true if parsing succeeds and false if it fails |
| 62 |
* @throws IOException if reading the contents fails |
| 63 |
*/ |
| 64 |
public boolean parse(final String path, InputStream input) |
| 65 |
throws IOException { |
| 66 |
// No need to validate framework files |
| 67 |
if (mIsFramework) { |
| 68 |
try { |
| 69 |
Closeables.close(input, true /* swallowIOException */); |
| 70 |
} catch (IOException e) { |
| 71 |
// cannot happen |
| 72 |
} |
| 73 |
return true; |
| 74 |
} |
| 75 |
if (mContext.needsFullAapt()) { |
| 76 |
try { |
| 77 |
Closeables.close(input, true /* swallowIOException */); |
| 78 |
} catch (IOException e) { |
| 79 |
// cannot happen |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
KXmlParser parser = new KXmlParser(); |
| 85 |
try { |
| 86 |
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); |
| 87 |
|
| 88 |
if (input instanceof FileInputStream) { |
| 89 |
input = new BufferedInputStream(input); |
| 90 |
} |
| 91 |
parser.setInput(input, SdkConstants.UTF_8); |
| 92 |
|
| 93 |
return parse(path, parser); |
| 94 |
} catch (XmlPullParserException e) { |
| 95 |
String message = e.getMessage(); |
| 96 |
|
| 97 |
// Strip off position description |
| 98 |
int index = message.indexOf("(position:"); //$NON-NLS-1$ (Hardcoded in KXml) |
| 99 |
if (index != -1) { |
| 100 |
message = message.substring(0, index); |
| 101 |
} |
| 102 |
|
| 103 |
String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$ |
| 104 |
path, parser.getLineNumber(), message); |
| 105 |
mContext.addError(error); |
| 106 |
return false; |
| 107 |
} catch (RuntimeException e) { |
| 108 |
// Some exceptions are thrown by the KXmlParser that are not XmlPullParserExceptions, |
| 109 |
// such as this one: |
| 110 |
// java.lang.RuntimeException: Undefined Prefix: w in org.kxml2.io.KXmlParser@... |
| 111 |
// at org.kxml2.io.KXmlParser.adjustNsp(Unknown Source) |
| 112 |
// at org.kxml2.io.KXmlParser.parseStartTag(Unknown Source) |
| 113 |
String message = e.getMessage(); |
| 114 |
String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$ |
| 115 |
path, parser.getLineNumber(), message); |
| 116 |
mContext.addError(error); |
| 117 |
return false; |
| 118 |
} finally { |
| 119 |
try { |
| 120 |
Closeables.close(input, true /* swallowIOException */); |
| 121 |
} catch (IOException e) { |
| 122 |
// cannot happen |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
private boolean parse(String path, KXmlParser parser) |
| 128 |
throws XmlPullParserException, IOException { |
| 129 |
boolean checkForErrors = !mIsFramework && !mContext.needsFullAapt(); |
| 130 |
|
| 131 |
while (true) { |
| 132 |
int event = parser.next(); |
| 133 |
if (event == XmlPullParser.START_TAG) { |
| 134 |
for (int i = 0, n = parser.getAttributeCount(); i < n; i++) { |
| 135 |
String attribute = parser.getAttributeName(i); |
| 136 |
String value = parser.getAttributeValue(i); |
| 137 |
assert value != null : attribute; |
| 138 |
|
| 139 |
if (checkForErrors) { |
| 140 |
String uri = parser.getAttributeNamespace(i); |
| 141 |
if (!mContext.checkValue(uri, attribute, value)) { |
| 142 |
mContext.requestFullAapt(); |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} else if (event == XmlPullParser.END_DOCUMENT) { |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
} |