use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class JSPContentSourceValidator method validate.
/**
* This validate call is for the ISourceValidator partial document
* validation approach
*
* @param dirtyRegion
* @param helper
* @param reporter
* @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator
*/
public void validate(IRegion dirtyRegion, IValidationContext helper, IReporter reporter) {
if (helper == null || fDocument == null || !fEnableSourceValidation)
return;
if ((reporter != null) && (reporter.isCancelled() == true)) {
throw new OperationCanceledException();
}
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument);
if (model == null)
// error
return;
try {
IDOMDocument document = null;
if (model instanceof IDOMModel) {
document = ((IDOMModel) model).getDocument();
}
if (document == null || !hasHTMLFeature(document))
// ignore
return;
ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument);
if (fb == null)
return;
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fb.getLocation());
if (file == null || !file.exists())
return;
// this will be the wrong region if it's Text (instead of Element)
// we don't know how to validate Text
// model.getIndexedRegion(dirtyRegion.getOffset());
IndexedRegion ir = getCoveringNode(dirtyRegion);
if (ir instanceof Text) {
while (ir != null && ir instanceof Text) {
// it's assumed that this gets the IndexedRegion to
// the right of the end offset
ir = model.getIndexedRegion(ir.getEndOffset());
}
}
if (ir instanceof INodeNotifier) {
INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance();
ValidationAdapter adapter = (ValidationAdapter) factory.adapt((INodeNotifier) ir);
if (adapter == null)
// error
return;
if (reporter != null) {
HTMLValidationReporter rep = null;
rep = getReporter(reporter, file, (IDOMModel) model);
rep.clear();
adapter.setReporter(rep);
Message mess = new LocalizedMessage(IMessage.LOW_SEVERITY, file.getFullPath().toString().substring(1));
reporter.displaySubtask(this, mess);
}
adapter.validate(ir);
}
} finally {
if (model != null)
model.releaseFromRead();
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class CleanupProcessorCSS method cleanupModel.
public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
if (structuredModel instanceof ICSSModel) {
ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
StringBuffer buf = formatter.cleanup(doc);
if (buf != null) {
int startOffset = ((IndexedRegion) doc).getStartOffset();
int endOffset = ((IndexedRegion) doc).getEndOffset();
formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
}
} else if (structuredModel instanceof IDOMModel) {
List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
if (cssnodes != null && !cssnodes.isEmpty()) {
ICSSModel model = null;
for (int i = 0; i < cssnodes.size(); i++) {
ICSSNode node = (ICSSNode) cssnodes.get(i);
CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
StringBuffer buf = formatter.cleanup(node);
if (buf != null) {
int startOffset = ((IndexedRegion) node).getStartOffset();
int endOffset = ((IndexedRegion) node).getEndOffset();
if (model == null) {
model = node.getOwnerDocument().getModel();
}
formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method quoteAttrValue.
protected IDOMNode quoteAttrValue(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
if (element.isCommentTag())
// do nothing
return node;
boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
IDOMNode newNode = node;
if (quoteAttrValues) {
NamedNodeMap attributes = newNode.getAttributes();
int attributesLength = attributes.getLength();
ISourceGenerator generator = node.getModel().getGenerator();
for (int i = 0; i < attributesLength; i++) {
attributes = newNode.getAttributes();
attributesLength = attributes.getLength();
IDOMAttr eachAttr = (IDOMAttr) attributes.item(i);
// ITextRegion oldAttrValueRegion = eachAttr.getValueRegion();
String oldAttrValue = eachAttr.getValueRegionText();
if (oldAttrValue == null) {
IDOMModel structuredModel = node.getModel();
if (isXMLType(structuredModel)) {
// TODO: Kit, please check. Is there any way to not
// rely on getting regions from attributes?
// $NON-NLS-1$ //$NON-NLS-2$
String newAttrValue = "=\"" + eachAttr.getNameRegionText() + "\"";
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, eachAttr.getNameRegionEndOffset(), 0, newAttrValue);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(node.getStartOffset());
// new
// node
}
} else {
char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : DOUBLE_QUOTE;
String newAttrValue = generator.generateAttrValue(eachAttr, quote);
// Workaround for now...
if (oldAttrValue.length() == 1) {
char firstChar = oldAttrValue.charAt(0);
if (firstChar == SINGLE_QUOTE)
newAttrValue = SINGLE_QUOTES;
else if (firstChar == DOUBLE_QUOTE)
newAttrValue = DOUBLE_QUOTES;
}
if (newAttrValue != null) {
if (newAttrValue.compareTo(oldAttrValue) != 0) {
int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
int attrValueLength = oldAttrValue.length();
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
// new
// node
}
}
}
}
}
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method compressEmptyElementTag.
/**
* <p>Compress empty element tags if the prefence is set to do so</p>
*
* @copyof org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag
*
* @param node the {@link IDOMNode} to possible compress
* @return the compressed node if the given node should be compressed, else the node as it was given
*/
private IDOMNode compressEmptyElementTag(IDOMNode node) {
boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
IDOMNode newNode = node;
IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
// only compress tags if they are empty
if ((compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null)) {
// only compress end tags if its XHTML or not a container
if (isXMLTag((IDOMElement) newNode) || !newNode.isContainer()) {
ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
ITextRegion lastRegion = regions.get(regions.size() - 1);
// format children and end tag if not empty element tag
if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
NodeList childNodes = newNode.getChildNodes();
if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
IDOMModel structuredModel = newNode.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
int startTagStartOffset = newNode.getStartOffset();
int offset = endTagStructuredDocumentRegion.getStart();
int length = endTagStructuredDocumentRegion.getLength();
// $NON-NLS-1$
structuredDocument.replaceText(structuredDocument, offset, length, "");
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
offset = startTagStructuredDocumentRegion.getStart() + lastRegion.getStart();
// $NON-NLS-1$
structuredDocument.replaceText(structuredDocument, offset, 0, "/");
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
}
}
}
}
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method applyAttrNameCase.
protected void applyAttrNameCase(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
if (element.isCommentTag())
// do nothing
return;
int attrNameCase = HTMLCorePreferenceNames.ASIS;
if (!shouldPreserveCase(element)) {
if (isXMLTag(element))
attrNameCase = HTMLCorePreferenceNames.LOWER;
else
attrNameCase = getCleanupPreferences().getAttrNameCase();
}
NamedNodeMap attributes = node.getAttributes();
int attributesLength = attributes.getLength();
for (int i = 0; i < attributesLength; i++) {
IDOMNode eachAttr = (IDOMNode) attributes.item(i);
if (hasNestedRegion(eachAttr.getNameRegion()))
continue;
String oldAttrName = eachAttr.getNodeName();
String newAttrName = oldAttrName;
/*
* 254961 - all HTML tag names and attribute names should be in
* English even for HTML files in other languages like Japanese or
* Turkish. English locale should be used to convert between
* uppercase and lowercase (otherwise "link" would be converted to
* Turkish "I Overdot Capital").
*/
if (attrNameCase == HTMLCorePreferenceNames.LOWER)
newAttrName = oldAttrName.toLowerCase(Locale.US);
else if (attrNameCase == HTMLCorePreferenceNames.UPPER)
newAttrName = oldAttrName.toUpperCase(Locale.US);
if (newAttrName.compareTo(oldAttrName) != 0) {
int attrNameStartOffset = eachAttr.getStartOffset();
int attrNameLength = oldAttrName.length();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, attrNameStartOffset, attrNameLength, newAttrName);
}
}
}
Aggregations