protected IStatus doSetPortalProperties( IDOMDocument document, IDataModel model, String propertiesFile) { // <hook> element Element rootElement = document.getDocumentElement(); // check for existing element Element portalPropertiesElement = null; NodeList nodeList = rootElement.getElementsByTagName("portal-properties"); // $NON-NLS-1$ if (nodeList != null && nodeList.getLength() > 0) { portalPropertiesElement = (Element) nodeList.item(0); NodeUtil.removeChildren(portalPropertiesElement); Node textNode = document.createTextNode(propertiesFile); portalPropertiesElement.appendChild(textNode); } else { portalPropertiesElement = NodeUtil.insertChildElement( rootElement, rootElement.getFirstChild(), "portal-properties", propertiesFile); //$NON-NLS-1$ } // format the new node added to the model; FormatProcessorXML processor = new FormatProcessorXML(); processor.formatNode(portalPropertiesElement); return Status.OK_STATUS; }
protected IStatus doAddActionItems(IDOMDocument document, List<String[]> actionItems) { // <hook> element Element rootElement = document.getDocumentElement(); FormatProcessorXML processor = new FormatProcessorXML(); Element newServiceElement = null; if (actionItems != null) { for (String[] actionItem : actionItems) { newServiceElement = NodeUtil.appendChildElement(rootElement, "service"); // $NON-NLS-1$ NodeUtil.appendChildElement( newServiceElement, "service-type", actionItem[0]); // $NON-NLS-1$ NodeUtil.appendChildElement( newServiceElement, "service-impl", actionItem[1]); // $NON-NLS-1$ processor.formatNode(newServiceElement); } if (newServiceElement != null) { // append a newline text node rootElement.appendChild( document.createTextNode(System.getProperty("line.separator"))); // $NON-NLS-1$ processor.formatNode(newServiceElement); } } return Status.OK_STATUS; }
public IStatus doSetCustomJSPDir(IDOMDocument document, IDataModel model) { // <hook> element Element rootElement = document.getDocumentElement(); final ILiferayProject lrproject = LiferayCore.create(project); final IPath defaultWebappRootFolderFullPath = lrproject.getDefaultDocrootFolder().getFullPath(); String customJSPsFolder = model.getStringProperty(CUSTOM_JSPS_FOLDER); String relativeJspFolderPath = ProjectUtil.getRelativePathFromDocroot( this.project, defaultWebappRootFolderFullPath.append(customJSPsFolder).toPortableString()); Element customJspElement = null; // check for existing element NodeList nodeList = rootElement.getElementsByTagName("custom-jsp-dir"); // $NON-NLS-1$ if (nodeList != null && nodeList.getLength() > 0) { customJspElement = (Element) nodeList.item(0); NodeUtil.removeChildren(customJspElement); Node textNode = document.createTextNode(relativeJspFolderPath); customJspElement.appendChild(textNode); } else { // need to insert customJspElement before any <service> NodeList serviceTags = rootElement.getElementsByTagName("service"); // $NON-NLS-1$ if (serviceTags != null && serviceTags.getLength() > 0) { customJspElement = NodeUtil.insertChildElement( rootElement, serviceTags.item(0), "custom-jsp-dir", relativeJspFolderPath); //$NON-NLS-1$ } else { customJspElement = NodeUtil.appendChildElement( rootElement, "custom-jsp-dir", relativeJspFolderPath); // $NON-NLS-1$ // append a newline text node rootElement.appendChild( document.createTextNode(System.getProperty("line.separator"))); // $NON-NLS-1$ } } // format the new node added to the model; FormatProcessorXML processor = new FormatProcessorXML(); processor.formatNode(customJspElement); return Status.OK_STATUS; }
protected void updateDtdVersion(IProject project, String dtdVersion, String archetypeVesion) { final String tmpPublicId = dtdVersion; final String tmpSystemId = dtdVersion.replaceAll("\\.", "_"); IStructuredModel editModel = null; final IFile[] metaFiles = getLiferayMetaFiles(project); for (IFile file : metaFiles) { try { editModel = StructuredModelManager.getModelManager().getModelForEdit(file); if (editModel != null && editModel instanceof IDOMModel) { final IDOMDocument xmlDocument = ((IDOMModel) editModel).getDocument(); final DocumentTypeImpl docType = (DocumentTypeImpl) xmlDocument.getDoctype(); final String publicId = docType.getPublicId(); final String newPublicId = getNewDoctTypeSetting(publicId, tmpPublicId, publicid_pattern); if (newPublicId != null) { docType.setPublicId(newPublicId); } final String systemId = docType.getSystemId(); final String newSystemId = getNewDoctTypeSetting(systemId, tmpSystemId, systemid_pattern); if (newSystemId != null) { docType.setSystemId(newSystemId); } editModel.save(); } } catch (Exception e) { final IStatus error = ProjectCore.createErrorStatus( "Unable to upgrade deployment meta file for " + file.getName(), e); ProjectCore.logError(error); } finally { if (editModel != null) { editModel.releaseFromEdit(); } } } ProjectCore.operate( project, UpdateDescriptorVersionOperation.class, archetypeVesion, dtdVersion); }
private Element getRepo(IDOMDocument pom, String id) { Element doc = pom.getDocumentElement(); Element repos = findChild(doc, REPOSITORIES); if (repos != null) { return findChild(repos, REPOSITORY, childEquals("id", id)); } return null; }
/** * Returns the namespace prefix for the given URI. * * @param doc document object model of the XML source file * @param namespaceUri namespace URI to examine * @return namespace prefix for the given URI */ public static String getPrefixForNamespaceUri(IDOMDocument doc, String namespaceUri) { if (doc != null && namespaceUri != null) { NamespaceTable table = new NamespaceTable(doc); Element elem = doc.getDocumentElement(); table.addElementLineage(elem); return table.getPrefixForURI(namespaceUri); } return null; }
/** * Find a bom element in pom based on its artifactId (can't use its own id as that isn't found * anywhere in the element's xml). */ private Element getBom(IDOMDocument pom, String aid) { Element depman = findChild(pom.getDocumentElement(), DEPENDENCY_MANAGEMENT); if (depman != null) { Element deps = findChild(depman, DEPENDENCIES); if (deps != null) { return findChild(deps, DEPENDENCY, childEquals(ARTIFACT_ID, aid)); } } return null; }
protected IStatus doAddLanguageProperties( IDOMDocument document, List<String> languageProperties) { // <hook> element Element rootElement = document.getDocumentElement(); FormatProcessorXML processor = new FormatProcessorXML(); Element newLanguageElement = null; // check if we have existing custom_dir Node refChild = null; NodeList nodeList = rootElement.getElementsByTagName("custom-jsp-dir"); // $NON-NLS-1$ if (nodeList != null && nodeList.getLength() > 0) { refChild = nodeList.item(0); } else { nodeList = rootElement.getElementsByTagName("service"); // $NON-NLS-1$ if (nodeList != null && nodeList.getLength() > 0) { refChild = nodeList.item(0); } } if (languageProperties != null) { for (String languageProperty : languageProperties) { newLanguageElement = NodeUtil.insertChildElement( rootElement, refChild, "language-properties", languageProperty); // $NON-NLS-1$ processor.formatNode(newLanguageElement); } if (newLanguageElement != null) { // append a newline text node rootElement.appendChild( document.createTextNode(System.getProperty("line.separator"))); // $NON-NLS-1$ processor.formatNode(newLanguageElement); } } return Status.OK_STATUS; }
private int getRepoCount(IDOMDocument pom) { Element reposEl = findChild(pom.getDocumentElement(), REPOSITORIES); if (reposEl != null) { List<Element> repos = findChilds(reposEl, REPOSITORY); if (repos != null) { return repos.size(); } } return 0; }
private int getBomCount(IDOMDocument pom) { Element depman = findChild(pom.getDocumentElement(), DEPENDENCY_MANAGEMENT); if (depman != null) { Element deps = findChild(depman, DEPENDENCIES); if (deps != null) { List<Element> boms = findChilds(deps, DEPENDENCY); if (boms != null) { return boms.size(); } } } return 0; }
/** * Returns the default namespace URI by searching the document for the namespace declaration, or * null if none found. * * @param doc document object model of the XML source file * @return default namespace URI of the given document */ public static String getDefaultNamespaceUri(IDOMDocument doc) { if (doc != null) { Element root = doc.getDocumentElement(); if (root != null) { NamedNodeMap attrs = root.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node item = attrs.item(i); String itemName = item.getLocalName(); if (itemName.equals(ATTR_DEFAULT_NAMESPACE)) { return item.getNodeValue(); } } } } return null; }
public String readCustomJSPFolder(IDOMDocument document, IDataModel model) { // <hook> element Element rootElement = document.getDocumentElement(); Element customJspElement = null; // check for existing element NodeList nodeList = rootElement.getElementsByTagName("custom-jsp-dir"); // $NON-NLS-1$ if (nodeList != null && nodeList.getLength() > 0) { customJspElement = (Element) nodeList.item(0); return customJspElement.getFirstChild().getNodeValue(); } return null; }
private IStatus updateVaadinLiferayPortletXMLTo62(IDOMDocument document) { Element rootElement = document.getDocumentElement(); NodeList portletNodes = rootElement.getElementsByTagName("portlet"); if (portletNodes.getLength() > 1) { Element lastPortletElement = (Element) portletNodes.item(portletNodes.getLength() - 1); Node rnpNode = NodeUtil.appendChildElement( lastPortletElement, "requires-namespaced-parameters", "false"); Node ajaxNode = NodeUtil.appendChildElement(lastPortletElement, "ajaxable", "false"); Node hpcNode = lastPortletElement.getElementsByTagName("header-portlet-css").item(0); Node fpjNode = lastPortletElement.getElementsByTagName("footer-portlet-javascript").item(0); lastPortletElement.replaceChild(rnpNode, hpcNode); lastPortletElement.replaceChild(ajaxNode, fpjNode); } return Status.OK_STATUS; }
/** * Parses the XML document for schema information and returns it as an array, paired by namespace * URI and schema version. * * @param doc document object model of the XML source file * @return array of paired namespace URIs and schema versions for the given document */ public static List<String> parseSchemaLocationAttr(IDOMDocument doc) { if (doc != null) { Element root = doc.getDocumentElement(); if (root != null) { String schemaLocationValue = root.getAttribute(ATTR_SCHEMA_LOCATION); if (schemaLocationValue != null) { // Remove all line breaks and tabs schemaLocationValue = schemaLocationValue.replaceAll("\\n|\\t|\\r", " "); // $NON-NLS-1$ //$NON-NLS-2$ // Remove any extra spaces schemaLocationValue = schemaLocationValue.replaceAll(" +", " "); // $NON-NLS-1$ //$NON-NLS-2$ // Trim any remaining whitespace on the ends schemaLocationValue = schemaLocationValue.trim(); // Split along spaces into just the schema content return Arrays.asList(schemaLocationValue.split(" ")); // $NON-NLS-1$ } } } return null; }
private boolean hasHTMLFeature(IDOMDocument document) { DocumentTypeAdapter adapter = (DocumentTypeAdapter) document.getAdapterFor(DocumentTypeAdapter.class); if (adapter == null) return false; return adapter.hasFeature(HTMLDocumentTypeConstants.HTML); }
void performValidation(IFile f, IReporter reporter, IStructuredModel model, boolean inBatch) { if (model instanceof IDOMModel) { IDOMModel domModel = (IDOMModel) model; JsTranslationAdapterFactory.setupAdapterFactory(domModel); IDOMDocument xmlDoc = domModel.getDocument(); JsTranslationAdapter translationAdapter = (JsTranslationAdapter) xmlDoc.getAdapterFor(IJsTranslation.class); // translationAdapter.resourceChanged(); IJsTranslation translation = translationAdapter.getJsTranslation(false); if (!reporter.isCancelled()) { translation.setProblemCollectingActive(true); translation.reconcileCompilationUnit(); List problems = translation.getProblems(); // only update task markers if the model is the same as what's on disk boolean updateTasks = !domModel.isDirty() && f != null && f.isAccessible(); if (updateTasks) { // remove old JavaScript task markers try { IMarker[] foundMarkers = f.findMarkers(JAVASCRIPT_TASK_MARKER_TYPE, true, IResource.DEPTH_ONE); for (int i = 0; i < foundMarkers.length; i++) { foundMarkers[i].delete(); } } catch (CoreException e) { Logger.logException(e); } } // if(!inBatch) reporter.removeAllMessages(this, f); // add new messages for (int i = 0; i < problems.size() && !reporter.isCancelled(); i++) { IProblem problem = (IProblem) problems.get(i); IMessage m = createMessageFromProblem(problem, f, translation, domModel.getStructuredDocument()); if (m != null) { if (problem.getID() == IProblem.Task) { if (updateTasks) { // add new JavaScript task marker try { IMarker task = f.createMarker(JAVASCRIPT_TASK_MARKER_TYPE); task.setAttribute(IMarker.LINE_NUMBER, new Integer(m.getLineNumber())); task.setAttribute(IMarker.CHAR_START, new Integer(m.getOffset())); task.setAttribute(IMarker.CHAR_END, new Integer(m.getOffset() + m.getLength())); task.setAttribute(IMarker.MESSAGE, m.getText()); task.setAttribute(IMarker.USER_EDITABLE, Boolean.FALSE); switch (m.getSeverity()) { case IMessage.HIGH_SEVERITY: { task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_HIGH)); task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); } break; case IMessage.LOW_SEVERITY: { task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_LOW)); task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); } break; default: { task.setAttribute(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL)); task.setAttribute(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); } } } catch (CoreException e) { Logger.logException(e); } } } else { reporter.addMessage(fMessageOriginator, m); } } } } } }
/** * generateStartTag method * * @return java.lang.String * @param element Element */ public String generateStartTag(Element element) { if (element == null) return null; ElementImpl impl = (ElementImpl) element; if (impl.isJSPTag()) { // check if JSP content type and JSP Document IDOMDocument document = (IDOMDocument) element.getOwnerDocument(); if (document != null && document.isJSPType()) { if (document.isJSPDocument() && !impl.hasChildNodes()) { impl.setJSPTag(false); } } else { impl.setJSPTag(false); } } if (impl.isCommentTag() && impl.getExistingAdapter(TagAdapter.class) == null) { CommentElementRegistry registry = CommentElementRegistry.getInstance(); registry.setupCommentElement(impl); } // first check if tag adapter exists TagAdapter adapter = (TagAdapter) impl.getExistingAdapter(TagAdapter.class); if (adapter != null) { String startTag = adapter.getStartTag(impl); if (startTag != null) return startTag; } StringBuffer buffer = new StringBuffer(); if (impl.isCommentTag()) { if (impl.isJSPTag()) buffer.append(JSPTag.COMMENT_OPEN); else buffer.append(COMMENT_OPEN); String tagName = generateTagName(element); if (tagName != null) buffer.append(tagName); } else if (impl.isJSPTag()) { buffer.append(JSPTag.TAG_OPEN); String tagName = generateTagName(element); if (tagName != null) buffer.append(tagName); if (impl.isContainer()) return buffer.toString(); // JSP container } else { buffer.append('<'); String tagName = generateTagName(element); if (tagName != null) buffer.append(tagName); } NamedNodeMap attributes = element.getAttributes(); int length = attributes.getLength(); for (int i = 0; i < length; i++) { AttrImpl attr = (AttrImpl) attributes.item(i); if (attr == null) continue; buffer.append(' '); String attrName = generateAttrName(attr); if (attrName != null) buffer.append(attrName); String attrValue = generateAttrValue(attr); if (attrValue != null) { // attr name only for HTML boolean and JSP buffer.append('='); buffer.append(attrValue); } } String closeTag = generateCloseTag(element); if (closeTag != null) buffer.append(closeTag); return buffer.toString(); }
/** Filters out every {@link IFile} which is has unknown root elements in its XML content. */ @Override protected Set<IFile> filterMatchingFiles(Set<IFile> files) { // if project is a java project remove bin dirs from the list Set<String> outputDirectories = new HashSet<String>(); IJavaProject javaProject = JdtUtils.getJavaProject(project); if (javaProject != null) { try { // add default output directory outputDirectories.add(javaProject.getOutputLocation().toString()); // add source folder specific output directories for (IClasspathEntry entry : javaProject.getRawClasspath()) { if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getOutputLocation() != null) { outputDirectories.add(entry.getOutputLocation().toString()); } } } catch (JavaModelException e) { BeansCorePlugin.log(e); } } Set<IFile> detectedFiles = new LinkedHashSet<IFile>(); for (IFile file : files) { boolean skip = false; // first check if the file sits in an output directory String path = file.getFullPath().toString(); for (String outputDirectory : outputDirectories) { if (path.startsWith(outputDirectory)) { skip = true; } } if (skip) { continue; } // check if the file is known Spring xml file IStructuredModel model = null; try { try { model = StructuredModelManager.getModelManager().getExistingModelForRead(file); } catch (RuntimeException e) { // sometimes WTP throws a NPE in concurrency situations } if (model == null) { model = StructuredModelManager.getModelManager().getModelForRead(file); } if (model != null) { IDOMDocument document = ((DOMModelImpl) model).getDocument(); if (document != null && document.getDocumentElement() != null) { String namespaceUri = document.getDocumentElement().getNamespaceURI(); if (applyNamespaceFilter(file, namespaceUri)) { detectedFiles.add(file); } } } } catch (IOException e) { BeansCorePlugin.log(e); } catch (CoreException e) { BeansCorePlugin.log(e); } finally { if (model != null) { model.releaseFromRead(); } } } return detectedFiles; }
@Override protected void configureJBossAppXml() { IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(earProjectName); IVirtualComponent component = ComponentCore.createComponent(project); IVirtualFolder folder = component.getRootFolder(); IFolder rootFolder = (IFolder) folder.getUnderlyingFolder(); IResource jbossAppXml = rootFolder.findMember("META-INF/jboss-app.xml"); if (jbossAppXml == null || !(jbossAppXml instanceof IFile) || !jbossAppXml.exists()) { return; } IModelManager manager = StructuredModelManager.getModelManager(); if (manager == null) { return; } IStructuredModel model = null; try { model = manager.getModelForEdit((IFile) jbossAppXml); if (model instanceof IDOMModel) { IDOMModel domModel = (IDOMModel) model; IDOMDocument document = domModel.getDocument(); Element root = document.getDocumentElement(); if (root == null) { return; } NodeList children = root.getChildNodes(); boolean strictAdded = false; Node firstChild = null; for (int i = 0; i < children.getLength(); i++) { Node currentNode = children.item(i); if (Node.ELEMENT_NODE == currentNode.getNodeType() && firstChild == null) { firstChild = currentNode; } if (Node.ELEMENT_NODE == currentNode.getNodeType() && MODULE_ORDER.equals(currentNode.getNodeName())) { setValue(document, currentNode, STRICT); strictAdded = true; } } if (!strictAdded) { Element moduleOrder = document.createElement(MODULE_ORDER); setValue(document, moduleOrder, STRICT); if (firstChild != null) { root.insertBefore(moduleOrder, firstChild); } else { root.appendChild(moduleOrder); } } model.save(); } } catch (CoreException e) { SeamCorePlugin.getDefault().logError(e); } catch (IOException e) { SeamCorePlugin.getDefault().logError(e); } finally { if (model != null) { model.releaseFromEdit(); } } try { new FormatProcessorXML().formatFile((IFile) jbossAppXml); } catch (IOException e) { SeamCorePlugin.getDefault().logError(e); } catch (CoreException e) { SeamCorePlugin.getDefault().logError(e); } }