📄 pythonnaturestore.java
字号:
synchronized (this) {
StringBuffer fBufferStr = new StringBuffer();
Node child = self.getFirstChild();
if (child != null) {
Node next = child.getNextSibling();
if (next == null) {
if(hasTextContent(child)){
String nodeValue = child.getNodeValue();
if(nodeValue != null){
traceFunc("END getTextContent - "+nodeValue);
return nodeValue;
}
}
traceFunc("END getTextContent - EMPTY");
return "";
}
if (fBufferStr == null){
fBufferStr = new StringBuffer();
}
else {
fBufferStr.setLength(0);
}
getTextContent(fBufferStr, self);
traceFunc("END getTextContent - "+fBufferStr);
return fBufferStr.toString();
}
traceFunc("END getTextContent - EMPTY");
return "";
}
}
// internal method taking a StringBuffer in parameter
private synchronized void getTextContent(StringBuffer buf, Node self) throws DOMException {
traceFunc("getTextContent");
synchronized (this) {
Node child = self.getFirstChild();
while (child != null) {
if (hasTextContent(child)) {
getTextContent(buf, child);
}
child = child.getNextSibling();
}
}
traceFunc("END getTextContent");
}
// internal method returning whether to take the given node's text content
private synchronized boolean hasTextContent(Node child) {
traceFunc("hasTextContent");
synchronized (this) {
boolean ret = child.getNodeType() != Node.COMMENT_NODE &&
child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE;
traceFunc("END hasTextContent "+ret);
return ret;
}
}
/**
* Retrieve the value of a path property from the Xml representation. If the property is not found in the Xml document, the eclipse persistent property of the same key is read and migrated to the
* xml representation.
*
* @param key
* @return the array of strings representing paths
* @throws CoreException
*/
private synchronized String[] getPathPropertyFromXml(QualifiedName key) throws CoreException {
traceFunc("getPathPropertyFromXml");
synchronized (this) {
try {
Node propertyNode = findPropertyNodeInXml(PYDEV_NATURE_PATH_PROPERTY, key);
if (propertyNode != null) {
traceFunc("END getPathPropertyFromXml");
return getChildValuesWithType(propertyNode, PYDEV_PATH);
}
traceFunc("END getPathPropertyFromXml (null)");
return null;
} catch (Exception e) {
traceFunc("END getPathPropertyFromXml (EXCEPTION)");
IStatus status = new Status(IStatus.ERROR, "PythonNatureStore", -1, e.toString(), e);
throw new CoreException(status);
}
}
}
/**
* Store a path property in the xml document and request the storage of changes. If the paths parameter is null the property is removed from the document.
*
* @param key
* @param paths
* @throws CoreException
*/
private synchronized void setPathPropertyToXml(QualifiedName key, String[] paths, boolean store) throws CoreException {
traceFunc("setPathPropertyToXml");
synchronized (this) {
try {
Node oldChild = findPropertyNodeInXml(PYDEV_NATURE_PATH_PROPERTY, key);
if (oldChild != null && paths == null) {
getRootNodeInXml().removeChild(oldChild);
doStore();
} else if (paths != null) {
// The property is not in the file and we need to set it
Node property = document.createElement(PYDEV_NATURE_PATH_PROPERTY);
Node propertyName = document.createAttribute(PYDEV_NATURE_PROPERTY_NAME);
propertyName.setNodeValue(getKeyString(key));
property.getAttributes().setNamedItem(propertyName);
addChildValuesWithType(property, PYDEV_PATH, paths);
if (oldChild == null) {
getRootNodeInXml().appendChild(property);
} else {
getRootNodeInXml().replaceChild(property, oldChild);
}
if (store) {
doStore();
}
}
} catch (Exception e) {
traceFunc("END setPathPropertyToXml (EXCEPTION)");
IStatus status = new Status(IStatus.ERROR, "PythonNatureStore", -1, e.toString(), e);
throw new CoreException(status);
}
}
traceFunc("END setPathPropertyToXml");
}
/**
* Serializes an Xml document to an array of bytes.
*
* @param doc
* @return the array of bytes representing the Xml document
* @throws IOException
* @throws TransformerException
*/
private synchronized byte[] serializeDocument(Document doc) throws IOException, TransformerException {
traceFunc("serializeDocument");
synchronized (this) {
ByteArrayOutputStream s = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
DOMSource source = new DOMSource(doc);
StreamResult outputTarget = new StreamResult(s);
transformer.transform(source, outputTarget);
traceFunc("END serializeDocument");
return s.toByteArray();
}
}
/**
* Stores the internal Xml representation as a project resource.
*
*/
private synchronized void doStore() {
traceFunc("doStore");
synchronized (this) {
try {
doStore(new NullProgressMonitor());
} catch (Exception e) {
throw new RuntimeException("Error storing pydev project descriptor:"+project, e);
}
}
traceFunc("END doStore");
}
public void resourceChanged(IResourceChangeEvent event) {
if(project == null){
return;
}
traceFunc("resourceChanged -- "+project.getName());
if(inInit){
traceFunc("END resourceChanged (inInit)");
return;
}
//not synched -- called when a file is changed
boolean doRebuild = false;
if (onIgnoreRefresh != 0) {
traceFunc("END resourceChanged (on ignore refresh)");
return;
}
if (onStoreJob != 0) {
traceFunc("END resourceChanged (on store)");
return; // we're on a store job from the store itself, this means that we must
// call the rebuildPath programatically (and that the resource change
// is actually already reflected in our document and file)
}
if(!project.isOpen()){
traceFunc("END resourceChanged (!open)");
return; // project closed... no need to do anything if the project is closed
}
IResourceDelta eventDelta = event.getDelta();
if(eventDelta == null){
traceFunc("END resourceChanged (eventDelta == null)");
return; //no delta here... move on
}
IResourceDelta delta = eventDelta.findMember(xmlFile.getFullPath());
if (delta != null) {
if (xmlFile.getModificationStamp() != modStamp) {
try {
if (loadFromFile()) {
doRebuild = true;
}
} catch (CoreException e) {
traceFunc("END resourceChanged (EXCEPTION)");
throw new RuntimeException(e);
}
}
}
if(doRebuild){
PythonNature nature = PythonNature.getPythonNature(project);
if (nature != null) {
nature.rebuildPath();
}
}
traceFunc("END resourceChanged -- rebuilt:"+doRebuild);
}
/**
* This is the function that actually stores the contents of the xml into the file with the configurations.
*/
private synchronized IStatus doStore(IProgressMonitor monitor) throws IOException, TransformerException, CoreException {
if(this.project == null){
return Status.OK_STATUS;
}
traceFunc("doStore");
if(inInit){
traceFunc("END doStore (inInit)");
return Status.OK_STATUS;
}
synchronized (this) {
try {
// that's because while we're in a store job, we don't want to listen to resource changes (we just
// want resource changes from the user).
if (onStoreJob != 0) {
traceFunc("END doStore (already in store)");
return Status.OK_STATUS;
}
onStoreJob++;
if (document == null) {
traceFunc("END doStore (document == null)");
return new Status(Status.ERROR, "PythonNatureStore.doStore", -2, "document == null", new RuntimeException("document == null"));
}
final ByteArrayInputStream is = new ByteArrayInputStream(serializeDocument(document));
PythonNatureStoreJob job = new PythonNatureStoreJob("Save .pydevproject", is);
if (ProjectModulesManager.IN_TESTS){
job.run(new NullProgressMonitor());
}else{
job.schedule();
}
traceFunc("END doStore");
return Status.OK_STATUS;
} finally {
onStoreJob--;
}
}
}
public void startInit() {
inInit = true;
}
public void endInit() {
inInit = false;
doStore();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -