📄 pythonnaturestore.java
字号:
}
private synchronized void migrateProperty(QualifiedName key) throws CoreException {
traceFunc("migrateProperty");
synchronized (this) {
// Nothing found, try to migrate from persistent property
String propertyVal = project.getPersistentProperty(key);
if (propertyVal != null) {
setPropertyToXml(key, propertyVal, false);
project.setPersistentProperty(key, (String) null);
}
}
traceFunc("END migrateProperty");
}
private synchronized void migratePath(QualifiedName key) throws CoreException {
traceFunc("migratePath");
synchronized (this) {
// Try to migrate from persistent property
String[] propertyVal = getArrayFromPathString(project.getPersistentProperty(key));
if (propertyVal != null) {
// set in the xml
setPathPropertyToXml(key, propertyVal, false);
// and remove from the project
project.setPersistentProperty(key, (String) null);
}
}
traceFunc("END migratePath");
}
/**
* Get the root node of the project description
*
* @return the root Node object
* @throws CoreException if root node is not present
*/
private synchronized Node getRootNodeInXml() {
traceFunc("getRootNodeInXml");
synchronized (this) {
Assert.isNotNull(document);
NodeList nodeList = document.getElementsByTagName(PYDEV_PROJECT_DESCRIPTION);
Node ret = null;
if (nodeList != null && nodeList.getLength() > 0) {
ret = nodeList.item(0);
}
traceFunc("END getRootNodeInXml -- "+ret);
if(ret != null){
return ret;
}
throw new RuntimeException(StringUtils.format("Error. Unable to get the %s tag by its name. Project: %s", PYDEV_PROJECT_DESCRIPTION, project));
}
}
/**
* Assemble a string representation of a QualifiedName typed key
*
* @param key
* @return the assembled string key representation
*/
private synchronized String getKeyString(QualifiedName key) {
traceFunc("getKeyString");
synchronized (this) {
String keyString = key.getQualifier() != null ? key.getQualifier() : "";
String ret = keyString + "." + key.getLocalName();
traceFunc("END getKeyString");
return ret;
}
}
/**
* Finds a property node as a direct child of the root node with the specified type and key.
*
* @param type
* @param key
* @return The property node or null if a node with the supplied key and type cannot be found.
* @throws CoreException
*/
private synchronized Node findPropertyNodeInXml(String type, QualifiedName key) {
traceFunc("findPropertyNodeInXml");
synchronized (this) {
Node root = getRootNodeInXml();
NodeList childNodes = root.getChildNodes();
if (childNodes != null && childNodes.getLength() > 0) {
String keyString = getKeyString(key);
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeName().equals(type)) {
NamedNodeMap attrs = child.getAttributes();
if (attrs != null && attrs.getLength() > 0) {
String name = attrs.getNamedItem(PYDEV_NATURE_PROPERTY_NAME).getNodeValue();
if (name != null && name.equals(keyString)) {
traceFunc("END findPropertyNodeInXml - "+child);
return child;
}
}
}
}
}
traceFunc("END findPropertyNodeInXml (null)");
return null;
}
}
/**
* Returns the text contents of a nodes' children. The children shall have the specified type.
*
* @param node
* @param type
* @return the array of strings with the text contents or null if the node has no children.
*/
private synchronized String[] getChildValuesWithType(Node node, String type) {
traceFunc("getChildValuesWithType");
synchronized (this) {
NodeList childNodes = node.getChildNodes();
if (childNodes != null && childNodes.getLength() > 0) {
List<String> result = new ArrayList<String>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node child = childNodes.item(i);
if (child.getNodeName().equals(type)) {
result.add(getTextContent(child));
}
}
String[] retval = new String[result.size()];
traceFunc("END getChildValuesWithType");
return result.toArray(retval);
}
traceFunc("END getChildValuesWithType (null)");
return null;
}
}
/**
* Add children to a node with specified type and text contents. For each values array element a new child is created.
*
* @param node
* @param type
* @param values
*/
private synchronized void addChildValuesWithType(Node node, String type, String[] values) {
traceFunc("addChildValuesWithType");
synchronized (this) {
assert (node != null);
assert (values != null);
assert (type != null);
for (int i = 0; i < values.length; i++) {
Node child = document.createElement(type);
setTextContent(values[i], child);
node.appendChild(child);
}
}
traceFunc("END addChildValuesWithType");
}
/**
* Convert an array of path strings to a single string separated by | characters.
*
* @param pathArray
* @return the assembled string of paths or null if the input was null
*/
private synchronized String getPathStringFromArray(String[] pathArray) {
traceFunc("getPathStringFromArray");
synchronized (this) {
if (pathArray != null) {
StringBuffer s = new StringBuffer("");
for (int i = 0; i < pathArray.length; i++) {
if (i > 0) {
s.append('|');
}
s.append(pathArray[i]);
}
traceFunc("END getPathStringFromArray");
return s.toString();
}
traceFunc("END getPathStringFromArray (null)");
return null;
}
}
/**
* Convert a single string of paths separated by | characters to an array of strings.
*
* @param pathString
* @return the splitted array of strings or null if the input was null
*/
private synchronized String[] getArrayFromPathString(String pathString) {
traceFunc("getArrayFromPathString");
synchronized (this) {
if (pathString != null) {
traceFunc("END getArrayFromPathString");
return pathString.split("\\|");
}
traceFunc("END getArrayFromPathString (null)");
return null;
}
}
/* (non-Javadoc)
* @see org.python.pydev.plugin.nature.IPythonNatureStore#getPropertyFromXml(org.eclipse.core.runtime.QualifiedName)
*/
public synchronized String getPropertyFromXml(QualifiedName key){
if(this.project == null){
return "";
}
traceFunc("getPropertyFromXml - "+key);
synchronized (this) {
checkLoad("getPropertyFromXml");
try {
Node propertyNode = findPropertyNodeInXml(PYDEV_NATURE_PROPERTY, key);
if (propertyNode != null) {
String ret = getTextContent(propertyNode);
traceFunc("END getPropertyFromXml -- "+ret);
return ret;
}
traceFunc("END getPropertyFromXml (null)");
return null;
} catch (Exception e) {
traceFunc("END getPropertyFromXml (EXCEPTION)");
throw new RuntimeException("Error on document:"+document+" project:"+project, e);
}
}
}
/* (non-Javadoc)
* @see org.python.pydev.plugin.nature.IPythonNatureStore#setPropertyToXml(org.eclipse.core.runtime.QualifiedName, java.lang.String, boolean)
*/
public synchronized void setPropertyToXml(QualifiedName key, String value, boolean store) throws CoreException {
traceFunc(StringUtils.format("setPropertyToXml key:%s value:%s store:%s", key, value, store));
synchronized (this) {
if(store){
checkLoad("setPropertyToXml");
}
try {
Node child = findPropertyNodeInXml(PYDEV_NATURE_PROPERTY, key);
if (child != null) {
if (value == null) {
// remove child from file
getRootNodeInXml().removeChild(child);
} else {
setTextContent(value, child);
}
doStore();
} else if (value != null) {
// The property is not in the file and we need to set it
Node property = document.createElement(PYDEV_NATURE_PROPERTY);
Node propertyName = document.createAttribute(PYDEV_NATURE_PROPERTY_NAME);
propertyName.setNodeValue(getKeyString(key));
property.getAttributes().setNamedItem(propertyName);
setTextContent(value, property);
getRootNodeInXml().appendChild(property);
if (store) {
doStore();
}
}
} catch (Exception e) {
traceFunc("END setPropertyToXml (EXCEPTION)");
IStatus status = new Status(IStatus.ERROR, "PythonNatureStore", -1, e.toString(), e);
throw new CoreException(status);
}
}
traceFunc("END setPropertyToXml");
}
/**
* This function was gotten as a copy of the Node.setTextContent, because this function
* is not available in java 1.4
*/
private synchronized void setTextContent(String textContent, Node self) throws DOMException {
traceFunc("setTextContent");
synchronized (this) {
// get rid of any existing children
Node child;
while ((child = self.getFirstChild()) != null) {
self.removeChild(child);
}
// create a Text node to hold the given content
if (textContent != null && textContent.length() != 0) {
self.appendChild(document.createTextNode(textContent));
}
}
traceFunc("END setTextContent");
}
private synchronized String getTextContent(Node self) throws DOMException {
traceFunc("getTextContent");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -