| 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.android.ide.common.rendering.api.ResourceValue; |
| 22 |
import com.android.ide.common.resources.ValueResourceParser.IValueResourceRepository; |
| 23 |
import com.android.resources.ResourceType; |
| 24 |
import com.google.common.io.Closeables; |
| 25 |
|
| 26 |
import org.kxml2.io.KXmlParser; |
| 27 |
import org.xmlpull.v1.XmlPullParser; |
| 28 |
import org.xmlpull.v1.XmlPullParserException; |
| 29 |
|
| 30 |
import java.io.BufferedInputStream; |
| 31 |
import java.io.FileInputStream; |
| 32 |
import java.io.IOException; |
| 33 |
import java.io.InputStream; |
| 34 |
|
| 35 |
/** |
| 36 |
* Parser for scanning an id-generating resource file such as a layout or a menu |
| 37 |
* file, which registers any ids it encounters with an |
| 38 |
* {@link IValueResourceRepository}, and which registers errors with a |
| 39 |
* {@link ScanningContext}. |
| 40 |
*/ |
| 41 |
public class IdResourceParser { |
| 42 |
private final IValueResourceRepository mRepository; |
| 43 |
private final boolean mIsFramework; |
| 44 |
private ScanningContext mContext; |
| 45 |
|
| 46 |
/** |
| 47 |
* Creates a new {@link IdResourceParser} |
| 48 |
* |
| 49 |
* @param repository value repository for registering resource declaration |
| 50 |
* @param context a context object with state for the current update, such |
| 51 |
* as a place to stash errors encountered |
| 52 |
* @param isFramework true if scanning a framework resource |
| 53 |
*/ |
| 54 |
public IdResourceParser( |
| 55 |
@NonNull IValueResourceRepository repository, |
| 56 |
@NonNull ScanningContext context, |
| 57 |
boolean isFramework) { |
| 58 |
mRepository = repository; |
| 59 |
mContext = context; |
| 60 |
mIsFramework = isFramework; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Parse the given input and register ids with the given |
| 65 |
* {@link IValueResourceRepository}. |
| 66 |
* |
| 67 |
* @param type the type of resource being scanned |
| 68 |
* @param path the full OS path to the file being parsed |
| 69 |
* @param input the input stream of the XML to be parsed (will be closed by this method) |
| 70 |
* @return true if parsing succeeds and false if it fails |
| 71 |
* @throws IOException if reading the contents fails |
| 72 |
*/ |
| 73 |
public boolean parse(ResourceType type, final String path, InputStream input) |
| 74 |
throws IOException { |
| 75 |
KXmlParser parser = new KXmlParser(); |
| 76 |
try { |
| 77 |
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); |
| 78 |
|
| 79 |
if (input instanceof FileInputStream) { |
| 80 |
input = new BufferedInputStream(input); |
| 81 |
} |
| 82 |
parser.setInput(input, SdkConstants.UTF_8); |
| 83 |
|
| 84 |
return parse(type, path, parser); |
| 85 |
} catch (XmlPullParserException e) { |
| 86 |
String message = e.getMessage(); |
| 87 |
|
| 88 |
// Strip off position description |
| 89 |
int index = message.indexOf("(position:"); //$NON-NLS-1$ (Hardcoded in KXml) |
| 90 |
if (index != -1) { |
| 91 |
message = message.substring(0, index); |
| 92 |
} |
| 93 |
|
| 94 |
String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$ |
| 95 |
path, parser.getLineNumber(), message); |
| 96 |
mContext.addError(error); |
| 97 |
return false; |
| 98 |
} catch (RuntimeException e) { |
| 99 |
// Some exceptions are thrown by the KXmlParser that are not XmlPullParserExceptions, |
| 100 |
// such as this one: |
| 101 |
// java.lang.RuntimeException: Undefined Prefix: w in org.kxml2.io.KXmlParser@... |
| 102 |
// at org.kxml2.io.KXmlParser.adjustNsp(Unknown Source) |
| 103 |
// at org.kxml2.io.KXmlParser.parseStartTag(Unknown Source) |
| 104 |
String message = e.getMessage(); |
| 105 |
String error = String.format("%1$s:%2$d: Error: %3$s", //$NON-NLS-1$ |
| 106 |
path, parser.getLineNumber(), message); |
| 107 |
mContext.addError(error); |
| 108 |
return false; |
| 109 |
} finally { |
| 110 |
try { |
| 111 |
Closeables.close(input, true /* swallowIOException */); |
| 112 |
} catch (IOException e) { |
| 113 |
// cannot happen |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private boolean parse(ResourceType type, String path, KXmlParser parser) |
| 119 |
throws XmlPullParserException, IOException { |
| 120 |
boolean valid = true; |
| 121 |
boolean checkForErrors = !mIsFramework && !mContext.needsFullAapt(); |
| 122 |
|
| 123 |
while (true) { |
| 124 |
int event = parser.next(); |
| 125 |
if (event == XmlPullParser.START_TAG) { |
| 126 |
for (int i = 0, n = parser.getAttributeCount(); i < n; i++) { |
| 127 |
String attribute = parser.getAttributeName(i); |
| 128 |
String value = parser.getAttributeValue(i); |
| 129 |
assert value != null : attribute; |
| 130 |
|
| 131 |
if (checkForErrors) { |
| 132 |
String uri = parser.getAttributeNamespace(i); |
| 133 |
if (!mContext.checkValue(uri, attribute, value)) { |
| 134 |
mContext.requestFullAapt(); |
| 135 |
checkForErrors = false; |
| 136 |
valid = false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (value.startsWith("@+")) { //$NON-NLS-1$ |
| 141 |
// Strip out the @+id/ or @+android:id/ section |
| 142 |
String id = value.substring(value.indexOf('/') + 1); |
| 143 |
ResourceValue newId = new ResourceValue(ResourceType.ID, id, |
| 144 |
mIsFramework); |
| 145 |
mRepository.addResourceValue(newId); |
| 146 |
} |
| 147 |
} |
| 148 |
} else if (event == XmlPullParser.END_DOCUMENT) { |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return valid; |
| 154 |
} |
| 155 |
} |