use of org.dom4j.Attribute in project atlas by alibaba.
the class ManifestFileUtils method updatePermission.
/**
* Set the permissions for the manifest
*
* @param document
* @param manifestOptions
*/
private static void updatePermission(Document document, ManifestOptions manifestOptions) throws IOException {
if (null == manifestOptions) {
return;
}
// Get the root node
Element root = document.getRootElement();
if (null != manifestOptions.getPermissionJsonFile() && manifestOptions.getPermissionJsonFile().exists()) {
String json = FileUtils.readFileToString(manifestOptions.getPermissionJsonFile());
Permission permission = JSON.parseObject(json, Permission.class);
List<Node> nodes = root.selectNodes("//permission");
List<Node> nodes1 = root.selectNodes("//uses-permission");
List<Node> nodes2 = root.selectNodes("//uses-feature");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
Item item = Permission.query(permission.getPermissions(), name);
if (null == item) {
logger.warn("[permission] remove " + name);
element.getParent().remove(element);
}
}
for (Node node : nodes1) {
Element element = (Element) node;
String name = element.attributeValue("name");
Item item = Permission.query(permission.getUses_permissions(), name);
if (null == item) {
logger.warn("[uses-permission] remove " + name);
element.getParent().remove(element);
}
}
for (Node node : nodes2) {
Element element = (Element) node;
String name = element.attributeValue("name");
if (StringUtils.isEmpty(name)) {
continue;
}
Item item = Permission.query(permission.getUses_features(), name);
if (null == item) {
logger.warn("[uses-feature] remove " + name);
element.getParent().remove(element);
} else {
// Modify the value
if (StringUtils.isNotEmpty(item.getValue())) {
Attribute required = element.attribute("required");
if (null != required) {
required.setValue(item.getValue());
}
// element.addAttribute(new QName("required", new Namespace("android", null)), item.getValue());
}
}
}
return;
}
if (null != manifestOptions.getPermissionListFile() && manifestOptions.getPermissionListFile().exists()) {
List<String> whiteList = FileUtils.readLines(manifestOptions.getPermissionListFile());
List<Node> nodes = new ArrayList<>();
nodes.addAll(root.selectNodes("//permission"));
nodes.addAll(root.selectNodes("//uses-permission"));
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
if (!whiteList.contains(name)) {
logger.warn("[permission] remove " + name);
element.getParent().remove(element);
}
}
return;
}
// Update custom permissions
if (manifestOptions.isRemoveCustomPermission()) {
List<? extends Node> nodes = root.selectNodes("//permission");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
boolean retain = false;
for (String systemPermission : SYSTEM_PERMISSION) {
if (name.startsWith(systemPermission)) {
retain = true;
break;
}
}
// If there is a custom permission
if (null != manifestOptions.getRetainPermissions() && manifestOptions.getRetainPermissions().size() > 0) {
for (String retainPermission : manifestOptions.getRetainPermissions()) {
if (name.startsWith(retainPermission)) {
retain = true;
break;
}
}
}
if (!retain) {
element.getParent().remove(element);
}
}
}
// Update system permissions
if (null != manifestOptions.getRemoveSystemPermissions() && manifestOptions.getRemoveSystemPermissions().size() > 0) {
List<? extends Node> nodes = root.selectNodes("//uses-permission");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
Set<String> removedPermissions = manifestOptions.getRemoveSystemPermissions();
if (removedPermissions.contains(name)) {
element.getParent().remove(element);
}
}
}
}
use of org.dom4j.Attribute in project atlas by alibaba.
the class ManifestFileUtils method addAndroidLabel.
private static void addAndroidLabel(Document document, boolean pushInstall) {
// Get the root node
Element root = document.getRootElement();
Element applicationElement = root.element("application");
Attribute attribute = applicationElement.attribute("android:extractNativeLibs");
if (attribute == null && pushInstall) {
applicationElement.addAttribute("android:extractNativeLibs", "false");
} else if (pushInstall) {
attribute.setValue("false");
} else if (!pushInstall && attribute != null) {
applicationElement.remove(attribute);
}
}
use of org.dom4j.Attribute in project atlas by alibaba.
the class ManifestFileUtils method updateApplicationElement.
/**
* Update the Application node information
*
* @param element
* @param replaceAttrs
* @param removeAttrs
*/
private static void updateApplicationElement(Element element, Map<String, String> replaceAttrs, List<String> removeAttrs) {
for (Map.Entry<String, String> entry : replaceAttrs.entrySet()) {
String key = entry.getKey();
key = StringUtils.substringAfter(key, ":");
Attribute attribute = element.attribute(key);
if (null != attribute) {
element.remove(attribute);
}
}
for (String key : removeAttrs) {
key = StringUtils.substringAfter(key, ":");
Attribute attribute = element.attribute(key);
if (null != attribute) {
element.remove(attribute);
}
}
}
use of org.dom4j.Attribute in project atlas by alibaba.
the class ManifestFileUtils method updatePreProcessManifestFile.
/**
* Update the libManifest file
*
* @param modifyManifest
* @param mainManifestFileObject param updateSdkVersion
* @param incremental
*/
public static void updatePreProcessManifestFile(File modifyManifest, File orgManifestFile, ManifestInfo mainManifestFileObject, boolean updateSdkVersion, boolean incremental) throws IOException, DocumentException {
// Read the XML file
Document document = XmlHelper.readXml(orgManifestFile);
// Get the root node
Element root = document.getRootElement();
if (updateSdkVersion) {
Element useSdkElement = root.element("uses-sdk");
if (null != useSdkElement) {
useSdkElement.getParent().remove(useSdkElement);
}
}
// First, we will manually process the tools:remove and tools:replace rules, and find that some of them are not necessarily possible through the ManifestMerge
Element applicationElement = root.element("application");
List<Node> supportVersion = root.selectNodes("//meta-data");
// Determines whether there is application and needs to be deleted
if (null != applicationElement) {
Attribute attribute = applicationElement.attribute("name");
if (null != attribute) {
applicationElement.remove(attribute);
}
}
for (Node node : supportVersion) {
Element element = (Element) node;
Attribute attribute = element.attribute("name");
if (attribute != null) {
if (attribute.getValue().equals("android.support.VERSION")) {
if (element.attribute("value") != null)
element.attribute("value").setValue("25.3.1");
}
}
}
// if (supportVersion!= null && supportVersion.size() > 0){
// if (supportVersion.("name");
// }
Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute();
List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute();
if (null != applicationElement) {
// Remove the tools properties from the lib project
List<Attribute> toRomoved = new ArrayList<Attribute>();
for (Attribute attribute : applicationElement.attributes()) {
if (attribute.getName().equals("replace") || attribute.getName().equals("remove")) {
// applicationElement.remove(attribute);
// applicationElement.attributes().remove(attribute);
toRomoved.add(attribute);
}
}
if (toRomoved.size() > 0) {
for (Attribute attribute : toRomoved) {
applicationElement.remove(attribute);
}
}
updateApplicationElement(applicationElement, replaceAttrs, removeAttrs);
}
// Handle the packageName TODO
String packageName = root.attributeValue("package");
if (StringUtils.isEmpty(packageName)) {
packageName = ManifestFileUtils.getPackage(orgManifestFile);
}
List<? extends Node> applicatNodes = root.selectNodes("//application");
for (Node node : applicatNodes) {
Element element = (Element) node;
Attribute attribute = element.attribute("name");
if (attribute != null) {
if (!attribute.getValue().startsWith(packageName)) {
attribute.setValue(packageName + attribute.getValue());
}
}
}
fillFullClazzName(root, packageName, "activity");
fillFullClazzName(root, packageName, "provider");
fillFullClazzName(root, packageName, "receiver");
fillFullClazzName(root, packageName, "service");
if (incremental) {
root.addNamespace("tools", "http://schemas.android.com/tools");
List<? extends Node> nodes = root.selectNodes("//application/*");
for (Node node : nodes) {
Element element = (Element) node;
element.addAttribute("tools:node", "replace");
}
}
XmlHelper.saveDocument(document, modifyManifest);
}
use of org.dom4j.Attribute in project zm-mailbox by Zimbra.
the class AttributeManager method loadAttrs.
private void loadAttrs(File file, Document doc) {
Element root = doc.getRootElement();
if (!root.getName().equals(E_ATTRS)) {
error(null, file, "root tag is not " + E_ATTRS);
return;
}
Map<Integer, String> idsSeen = new HashMap<Integer, String>();
String group = root.attributeValue(A_GROUP);
String groupIdStr = root.attributeValue(A_GROUP_ID);
if (group == null ^ groupIdStr == null) {
error(null, file, A_GROUP + " and " + A_GROUP_ID + " both have to be both specified");
}
int groupId = -1;
if (group != null) {
try {
groupId = Integer.valueOf(groupIdStr);
} catch (NumberFormatException nfe) {
error(null, file, A_GROUP_ID + " is not a number: " + groupIdStr);
}
}
if (groupId == 2) {
error(null, file, A_GROUP_ID + " is not valid (used by ZimbraObjectClass)");
} else if (groupId > 0) {
if (mGroupMap.containsKey(groupId)) {
error(null, file, "duplicate group id: " + groupId);
} else if (mGroupMap.containsValue(group)) {
error(null, file, "duplicate group: " + group);
} else {
mGroupMap.put(groupId, group);
}
}
NEXT_ATTR: for (Iterator iter = root.elementIterator(); iter.hasNext(); ) {
Element eattr = (Element) iter.next();
if (!eattr.getName().equals(E_ATTR)) {
error(null, file, "unknown element: " + eattr.getName());
continue;
}
AttributeCallback callback = null;
AttributeType type = null;
AttributeOrder order = null;
String value = null;
String min = null;
String max = null;
boolean immutable = false;
// boolean ignore = false;
int id = -1;
String parentOid = null;
AttributeCardinality cardinality = null;
Set<AttributeClass> requiredIn = null;
Set<AttributeClass> optionalIn = null;
Set<AttributeFlag> flags = null;
String canonicalName = null;
String name = eattr.attributeValue(A_NAME);
if (name == null) {
error(null, file, "no name specified");
continue;
}
canonicalName = name.toLowerCase();
List<AttributeServerType> requiresRestart = null;
Version deprecatedSinceVer = null;
List<Version> sinceVer = null;
Boolean ephemeral = false;
for (Iterator attrIter = eattr.attributeIterator(); attrIter.hasNext(); ) {
Attribute attr = (Attribute) attrIter.next();
String aname = attr.getName();
if (aname.equals(A_NAME)) {
// nothing to do - already processed
} else if (aname.equals(A_CALLBACK)) {
callback = loadCallback(attr.getValue());
} else if (aname.equals(A_IMMUTABLE)) {
immutable = "1".equals(attr.getValue());
} else if (aname.equals(A_MAX)) {
max = attr.getValue();
} else if (aname.equals(A_MIN)) {
min = attr.getValue();
} else if (aname.equals(A_TYPE)) {
type = AttributeType.getType(attr.getValue());
if (type == null) {
error(name, file, "unknown <attr> type: " + attr.getValue());
continue NEXT_ATTR;
}
} else if (aname.equals(A_VALUE)) {
value = attr.getValue();
} else if (aname.equals(A_PARENT_OID)) {
parentOid = attr.getValue();
if (!parentOid.matches("^\\d+(\\.\\d+)+"))
error(name, file, "invalid parent OID " + parentOid + ": must be an OID");
} else if (aname.equals(A_ID)) {
try {
id = Integer.parseInt(attr.getValue());
if (id < 0) {
error(name, file, "invalid id " + id + ": must be positive");
}
} catch (NumberFormatException nfe) {
error(name, file, aname + " is not a number: " + attr.getValue());
}
} else if (aname.equals(A_CARDINALITY)) {
try {
cardinality = AttributeCardinality.valueOf(attr.getValue());
} catch (IllegalArgumentException iae) {
error(name, file, aname + " is not valid: " + attr.getValue());
}
} else if (aname.equals(A_REQUIRED_IN)) {
requiredIn = parseClasses(name, file, attr.getValue());
} else if (aname.equals(A_OPTIONAL_IN)) {
optionalIn = parseClasses(name, file, attr.getValue());
} else if (aname.equals(A_FLAGS)) {
flags = parseFlags(name, file, attr.getValue());
} else if (aname.equals(A_ORDER)) {
try {
order = AttributeOrder.valueOf(attr.getValue());
} catch (IllegalArgumentException iae) {
error(name, file, aname + " is not valid: " + attr.getValue());
}
} else if (aname.equals(A_REQUIRES_RESTART)) {
requiresRestart = parseRequiresRestart(name, file, attr.getValue());
} else if (aname.equals(A_DEPRECATED_SINCE)) {
String depreSince = attr.getValue();
if (depreSince != null) {
try {
deprecatedSinceVer = new Version(depreSince);
} catch (ServiceException e) {
error(name, file, aname + " is not valid: " + attr.getValue() + " (" + e.getMessage() + ")");
}
}
} else if (aname.equals(A_SINCE)) {
String since = attr.getValue();
if (since != null) {
try {
String[] versions = since.split(",");
sinceVer = new ArrayList<Version>();
for (String verStr : versions) {
sinceVer.add(new Version(verStr.trim()));
}
} catch (ServiceException e) {
error(name, file, aname + " is not valid: " + attr.getValue() + " (" + e.getMessage() + ")");
}
}
} else {
error(name, file, "unknown <attr> attr: " + aname);
}
}
List<String> globalConfigValues = new LinkedList<String>();
// note: init to null instead of empty List
List<String> globalConfigValuesUpgrade = null;
List<String> defaultCOSValues = new LinkedList<String>();
List<String> defaultExternalCOSValues = new LinkedList<String>();
// note: init to null instead of empty List
List<String> defaultCOSValuesUpgrade = null;
String description = null;
String deprecateDesc = null;
for (Iterator elemIter = eattr.elementIterator(); elemIter.hasNext(); ) {
Element elem = (Element) elemIter.next();
if (elem.getName().equals(E_GLOBAL_CONFIG_VALUE)) {
globalConfigValues.add(elem.getText());
} else if (elem.getName().equals(E_GLOBAL_CONFIG_VALUE_UPGRADE)) {
if (globalConfigValuesUpgrade == null)
globalConfigValuesUpgrade = new LinkedList<String>();
globalConfigValuesUpgrade.add(elem.getText());
} else if (elem.getName().equals(E_DEFAULT_COS_VALUE)) {
defaultCOSValues.add(elem.getText());
} else if (elem.getName().equals(E_DEFAULT_EXTERNAL_COS_VALUE)) {
defaultExternalCOSValues.add(elem.getText());
} else if (elem.getName().equals(E_DEFAULT_COS_VALUE_UPGRADE)) {
if (defaultCOSValuesUpgrade == null)
defaultCOSValuesUpgrade = new LinkedList<String>();
defaultCOSValuesUpgrade.add(elem.getText());
} else if (elem.getName().equals(E_DESCRIPTION)) {
if (description != null) {
error(name, file, "more than one " + E_DESCRIPTION);
}
description = elem.getText();
} else if (elem.getName().equals(E_DEPRECATE_DESC)) {
if (deprecateDesc != null) {
error(name, file, "more than one " + E_DEPRECATE_DESC);
}
deprecateDesc = elem.getText();
} else {
error(name, file, "unknown element: " + elem.getName());
}
}
if (deprecatedSinceVer != null && deprecateDesc == null)
error(name, file, "missing element " + E_DEPRECATE_DESC);
else if (deprecatedSinceVer == null && deprecateDesc != null)
error(name, file, "missing attr " + A_DEPRECATED_SINCE);
if (deprecatedSinceVer != null) {
String deprecateInfo = "Deprecated since: " + deprecatedSinceVer.toString() + ". " + deprecateDesc;
if (description == null)
description = deprecateInfo;
else
description = deprecateInfo + ". Orig desc: " + description;
}
// since is required after(inclusive) oid 525 - first attribute in 5.0
if (sinceVer == null && id >= 525) {
error(name, file, "missing since (required after(inclusive) oid 710)");
}
// Check that if id is specified, then cardinality is specified.
if (id > 0 && cardinality == null) {
error(name, file, "cardinality not specified");
}
// Check that if id is specified, then at least one object class is defined
if (id > 0 && (optionalIn != null && optionalIn.isEmpty()) && (requiredIn != null && requiredIn.isEmpty())) {
error(name, file, "atleast one of " + A_REQUIRED_IN + " or " + A_OPTIONAL_IN + " must be specified");
}
// Check that if id is specified, it must be unique
if (id > 0) {
String idForAttr = idsSeen.get(Integer.valueOf(id));
if (idForAttr != null) {
error(name, file, "duplicate id: " + id + " is already used for " + idForAttr);
} else {
idsSeen.put(Integer.valueOf(id), name);
}
}
// Check that if it is COS inheritable it is in account and COS classes
checkFlag(name, file, flags, AttributeFlag.accountInherited, AttributeClass.account, AttributeClass.cos, null, requiredIn, optionalIn);
// Check that if it is COS-domain inheritable it is in account and COS and domain classes
checkFlag(name, file, flags, AttributeFlag.accountCosDomainInherited, AttributeClass.account, AttributeClass.cos, AttributeClass.domain, requiredIn, optionalIn);
// Check that if it is domain inheritable it is in domain and global config
checkFlag(name, file, flags, AttributeFlag.domainInherited, AttributeClass.domain, AttributeClass.globalConfig, null, requiredIn, optionalIn);
// Check that if it is server inheritable it is in server and global config
checkFlag(name, file, flags, AttributeFlag.serverInherited, AttributeClass.server, AttributeClass.globalConfig, null, requiredIn, optionalIn);
// Check that if it is serverPreferAlwaysOn it is in server and alwaysOnCluster
checkFlag(name, file, flags, AttributeFlag.serverPreferAlwaysOn, AttributeClass.server, AttributeClass.alwaysOnCluster, null, requiredIn, optionalIn);
// default value is specified
if (cardinality == AttributeCardinality.single) {
if (globalConfigValues.size() > 1) {
error(name, file, "more than one global config value specified for cardinality " + AttributeCardinality.single);
}
if (defaultCOSValues.size() > 1 || defaultExternalCOSValues.size() > 1) {
error(name, file, "more than one default COS value specified for cardinality " + AttributeCardinality.single);
}
}
checkEphemeralFlags(name, file, flags, min, max, cardinality);
AttributeInfo info = createAttributeInfo(name, id, parentOid, groupId, callback, type, order, value, immutable, min, max, cardinality, requiredIn, optionalIn, flags, globalConfigValues, defaultCOSValues, defaultExternalCOSValues, globalConfigValuesUpgrade, defaultCOSValuesUpgrade, mMinimize ? null : description, requiresRestart, sinceVer, deprecatedSinceVer);
if (mAttrs.get(canonicalName) != null) {
error(name, file, "duplicate definiton");
}
mAttrs.put(canonicalName, info);
if (flags != null) {
for (AttributeFlag flag : flags) {
mFlagToAttrsMap.get(flag).add(name);
if (flag == AttributeFlag.accountCosDomainInherited)
mFlagToAttrsMap.get(AttributeFlag.accountInherited).add(name);
}
}
if (requiredIn != null || optionalIn != null) {
if (requiredIn != null) {
for (AttributeClass klass : requiredIn) {
mClassToAttrsMap.get(klass).add(name);
mClassToLowerCaseAttrsMap.get(klass).add(name.toLowerCase());
}
}
if (optionalIn != null) {
for (AttributeClass klass : optionalIn) {
mClassToAttrsMap.get(klass).add(name);
mClassToLowerCaseAttrsMap.get(klass).add(name.toLowerCase());
}
}
}
if (isBinaryType(type)) {
mBinaryAttrs.add(canonicalName);
} else if (isBinaryTransferType(type)) {
mBinaryTransferAttrs.add(canonicalName);
}
if (flags != null && flags.contains(AttributeFlag.ephemeral)) {
mEphemeralAttrs.put(canonicalName, info);
mEphemeralAttrsSet.add(name);
if (!info.isDynamic()) {
addNonDynamicEphemeralAttr(info);
}
}
}
}
Aggregations